Skip to content

Commit

Permalink
fix: improve create presigned urls concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
mateopresacastro committed Dec 16, 2024
1 parent f9883a7 commit 1bdf9cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
33 changes: 8 additions & 25 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,15 @@ export async function createPreSignedUrlAction(numOfSamples: number) {
);
}

// TODO optimize this
const zipFileSignedUrl = await createPresignedUrl({
const zipFileSignedUrlPromise = createPresignedUrl({
bucketName: AWS_PRIVATE_BUCKET_NAME,
fileType: "zip",
});
if (!zipFileSignedUrl) throw new Error("Error creating zip signed URL");

const imageSignedUrl = await createPresignedUrl({
const imageSignedUrlPromise = createPresignedUrl({
bucketName: AWS_PUBLIC_BUCKET_NAME,
fileType: "image",
});
if (!imageSignedUrl) throw new Error("Error creating image signed URL");

const samplesSignedUrlsPromises = new Array(numOfSamples)
.fill(null)
Expand All @@ -273,26 +270,12 @@ export async function createPreSignedUrlAction(numOfSamples: number) {
})
);

const samplesSignedUrlsSettled = await Promise.allSettled(
samplesSignedUrlsPromises
);

if (
samplesSignedUrlsSettled.some(
(result) => result.status === "rejected" || !result.value
)
) {
throw new Error("Error creating samples signed URLs");
}

const samplesSignedUrls = samplesSignedUrlsSettled
.filter(
(
result
): result is PromiseFulfilledResult<{ url: string; key: string }> =>
result.status === "fulfilled"
)
.map((result) => result.value);
const [zipFileSignedUrl, imageSignedUrl, ...samplesSignedUrls] =
await Promise.all([
zipFileSignedUrlPromise,
imageSignedUrlPromise,
...samplesSignedUrlsPromises,
]);

return {
zipFileSignedUrl,
Expand Down
29 changes: 12 additions & 17 deletions src/aws/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,18 @@ export async function createPresignedUrl({
bucketName: string;
fileType: "image" | "samples" | "zip";
}) {
try {
const randomPrefix = crypto.randomBytes(16).toString("hex");
const key = `uploads/${fileType}/${randomPrefix}`;
const command = new PutObjectCommand({
Bucket: bucketName,
Key: key,
});

const url = await getSignedUrl(s3, command, {
expiresIn: FIVE_MIN_IN_SECONDS,
});

return { url, key };
} catch (error) {
await log.error("Error creating presigned URL:", error);
return null;
}
const randomPrefix = crypto.randomBytes(16).toString("hex");
const key = `uploads/${fileType}/${randomPrefix}`;
const command = new PutObjectCommand({
Bucket: bucketName,
Key: key,
});

const url = await getSignedUrl(s3, command, {
expiresIn: FIVE_MIN_IN_SECONDS,
});

return { url, key };
}

export async function getObject({
Expand Down

0 comments on commit 1bdf9cc

Please sign in to comment.