From 3fe976dbcde702bfda7f5772ef46cee0decdbe1a Mon Sep 17 00:00:00 2001 From: David Welch Date: Tue, 29 Jul 2025 15:00:24 -0600 Subject: [PATCH] ops: s3 index script --- scripts/s3index.mjs | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 scripts/s3index.mjs diff --git a/scripts/s3index.mjs b/scripts/s3index.mjs new file mode 100644 index 0000000..b011bc5 --- /dev/null +++ b/scripts/s3index.mjs @@ -0,0 +1,70 @@ +/** + * Extracts all objects from an S3 bucket and generates an HTML index file. + */ +import { S3Client, ListObjectsV2Command, PutObjectCommand } from "@aws-sdk/client-s3"; + +const BUCKET = process.env.ARCHIVE_S3_BUCKET || "s3-cve"; +const REGION = process.env.AWS_REGION || "us-west-2"; + +const s3 = new S3Client({ region: REGION }); + +async function listAllObjects(bucket) { + let contents = []; + let continuationToken; + + do { + const res = await s3.send(new ListObjectsV2Command({ + Bucket: bucket, + ContinuationToken: continuationToken + })); + contents = contents.concat(res.Contents || []); + continuationToken = res.IsTruncated ? res.NextContinuationToken : null; + } while (continuationToken); + + return contents.map(obj => obj.Key); +} + +function generateHtml(keys) { + const updated = new Date().toISOString(); + const links = keys.map(k => `
  • ${k}
  • `).join("\n"); + return ` + +Index of ${BUCKET} + +

    Index of ${BUCKET}

    + +

    Updated: ${updated}

    + +`; +} + + +/** + * Upload a string as index.html to the given bucket + */ +async function uploadIndex(bucket, region, htmlString) { + const s3 = new S3Client({ region }); + const cmd = new PutObjectCommand({ + Bucket: bucket, + Key: "index.html", + Body: htmlString, + ContentType: "text/html; charset=utf-8" + }); + + await s3.send(cmd); + console.log(`Uploaded index.html to s3://${bucket}/index.html`); +} + + +(async () => { + try { + const keys = await listAllObjects(BUCKET); + const html = generateHtml(keys); + await uploadIndex(BUCKET, REGION, html); + } catch (err) { + console.error("Error generating index:", err); + process.exit(1); + } +})();