From 75090f1ebaaead874a7aaa0cefa845d6aeaaefa6 Mon Sep 17 00:00:00 2001 From: jtsmedley <38006759+jtsmedley@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:05:25 -0500 Subject: [PATCH] Add support for generating bucket CID (#11) --- src/bucketManager.js | 35 ++++++++++++++++++++++++++++++++++- test/bucketManager.spec.cjs | 23 +++++++++++++++++++++++ test/bucketManager.spec.mjs | 23 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/bucketManager.js b/src/bucketManager.js index e248ca3..f09c1ac 100644 --- a/src/bucketManager.js +++ b/src/bucketManager.js @@ -3,7 +3,7 @@ import { DeleteBucketCommand, GetBucketAclCommand, ListBucketsCommand, - PutBucketAclCommand, + PutBucketAclCommand, PutBucketTaggingCommand, S3Client, } from "@aws-sdk/client-s3"; @@ -130,6 +130,39 @@ class BucketManager { }); return !(typeof readPermission !== "undefined"); } + + /** + * @summary Generates the IPFS Directory/Folder CID for a given bucket + * @param {string} name - The name of the bucket to use. + * @returns {Promise} A promise that resolves with the CID of the new directory/folder + */ + async generateCid(name) { + const command = new PutBucketTaggingCommand({ + Bucket: name, + Tagging: { + TagSet: [ + { + Key: "generateBucketCid", + Value: "true" + } + ] + } + }); + + let cid = false; + command.middlewareStack.add( + (next) => async (args) => { + const response = await next(args); + + // Get cid from headers + cid = response.response.headers["x-amz-meta-cid"]; + return response; + } + ); + + await this.#client.send(command); + return cid; + } } export default BucketManager; diff --git a/test/bucketManager.spec.cjs b/test/bucketManager.spec.cjs index 47018a8..8d97455 100644 --- a/test/bucketManager.spec.cjs +++ b/test/bucketManager.spec.cjs @@ -30,6 +30,29 @@ test("create bucket", async () => { } }); +test("generate bucket cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-generate-bucket-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // Generate bucket CID + const generatedCid = await bucketManager.generateCid(bucketNameToGenerate); + + // Assert new bucket exists + assert.equal(generatedCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + test("list buckets", async () => { const testBucketName = `${TEST_PREFIX}-list-bucket-test-pass`, bucketManager = new BucketManager( diff --git a/test/bucketManager.spec.mjs b/test/bucketManager.spec.mjs index 54a84a0..5ed3f83 100644 --- a/test/bucketManager.spec.mjs +++ b/test/bucketManager.spec.mjs @@ -30,6 +30,29 @@ test("create bucket", async () => { } }); +test("generate bucket cid", async () => { + // Initialize BucketManager + const bucketManager = new BucketManager( + process.env.TEST_S3_KEY || process.env.TEST_KEY, + process.env.TEST_S3_SECRET || process.env.TEST_SECRET, + ); + + // Create bucket `create-bucket-test-pass` + const bucketNameToGenerate = `${TEST_PREFIX}-generate-bucket-test-pass`; + await bucketManager.create(bucketNameToGenerate); + + try { + // Generate bucket CID + const generatedCid = await bucketManager.generateCid(bucketNameToGenerate); + + // Assert new bucket exists + assert.equal(generatedCid, "bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"); + } finally { + // Delete new bucket + await bucketManager.delete(bucketNameToGenerate); + } +}); + test("list buckets", async () => { const testBucketName = `${TEST_PREFIX}-list-bucket-test-pass`, bucketManager = new BucketManager(