Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/lib/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,48 @@ export interface BundleHistory {
history: BundleEvent[];
}

/**
* Lists every object key under `prefix` (ListObjectsV2: up to 1000 keys per page, uses continuation).
*/
export async function listAllObjectKeysForPrefix(
s3: Pick<S3Client, "send">,
bucket: string,
prefix: string,
): Promise<string[]> {
const keys: string[] = [];
let continuationToken: string | undefined;

for (;;) {
const listResponse = await s3.send(
new ListObjectsV2Command({
Bucket: bucket,
Prefix: prefix,
ContinuationToken: continuationToken,
}),
);

for (const obj of listResponse.Contents ?? []) {
if (obj.Key) {
keys.push(obj.Key);
}
}

if (!listResponse.IsTruncated || !listResponse.NextContinuationToken) {
break;
}
continuationToken = listResponse.NextContinuationToken;
}

return keys;
}

export async function getBundleHistory(
bundleKey: string,
): Promise<BundleHistory | null> {
const prefix = `bundles/${bundleKey}/`;
const listCommand = new ListObjectsV2Command({
Bucket: BUCKET_NAME,
Prefix: prefix,
});

const listResponse = await s3Client.send(listCommand);
const keys = listResponse.Contents?.map((obj) => obj.Key).filter(
Boolean,
) as string[];
const keys = await listAllObjectKeysForPrefix(s3Client, BUCKET_NAME, prefix);

if (!keys || keys.length === 0) {
if (keys.length === 0) {
return null;
}

Expand Down