Skip to content

Commit

Permalink
Add support for generating bucket CID
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsmedley committed Sep 17, 2024
1 parent 1b6fa88 commit f117ce6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/bucketManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
DeleteBucketCommand,
GetBucketAclCommand,
ListBucketsCommand,
PutBucketAclCommand,
PutBucketAclCommand, PutBucketTaggingCommand,
S3Client,
} from "@aws-sdk/client-s3";

Expand Down Expand Up @@ -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<boolean>} 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;
23 changes: 23 additions & 0 deletions test/bucketManager.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions test/bucketManager.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit f117ce6

Please sign in to comment.