-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbucketManager.js
168 lines (148 loc) · 4.92 KB
/
bucketManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
CreateBucketCommand,
DeleteBucketCommand,
GetBucketAclCommand,
ListBucketsCommand,
PutBucketAclCommand, PutBucketTaggingCommand,
S3Client,
} from "@aws-sdk/client-s3";
/** Provides methods for managing buckets in an S3 endpoint. */
class BucketManager {
#DEFAULT_ENDPOINT = "https://s3.filebase.com";
#DEFAULT_REGION = "us-east-1";
#client;
/**
* @summary Creates a new instance of the constructor.
* @param {string} clientKey - The access key ID for authentication.
* @param {string} clientSecret - The secret access key for authentication.
* @tutorial quickstart-bucket
* @example
* import { BucketManager } from "@filebase/sdk";
* const bucketManager = new BucketManager("KEY_FROM_DASHBOARD", "SECRET_FROM_DASHBOARD");
*/
constructor(clientKey, clientSecret) {
const clientEndpoint =
process.env.NODE_ENV === "test"
? process.env.TEST_S3_ENDPOINT || this.#DEFAULT_ENDPOINT
: this.#DEFAULT_ENDPOINT,
clientConfiguration = {
credentials: {
accessKeyId: clientKey,
secretAccessKey: clientSecret,
},
endpoint: clientEndpoint,
region: this.#DEFAULT_REGION,
forcePathStyle: true,
};
this.#client = new S3Client(clientConfiguration);
}
/**
* @typedef {Object} bucket
* @property {string} Name The name of the bucket
* @property {date} Date the bucket was created
*/
/**
* @summary Creates a new bucket with the specified name.
* @param {string} name - The name of the bucket to create.
* @returns {Promise<bucket>} - A promise that resolves when the bucket is created.
* @example
* // Create bucket with name of `create-bucket-example`
* await bucketManager.create(`create-bucket-example`);
*/
async create(name) {
const command = new CreateBucketCommand({
Bucket: name,
});
return await this.#client.send(command);
}
/**
* @summary Lists the buckets in the client.
* @returns {Promise<Array<bucket>>} - A promise that resolves with an array of objects representing the buckets in the client.
* @example
* // List all buckets
* await bucketManager.list();
*/
async list() {
const command = new ListBucketsCommand({}),
{ Buckets } = await this.#client.send(command);
return Buckets;
}
/**
* @summary Deletes the specified bucket.
* @param {string} name - The name of the bucket to delete.
* @returns {Promise<boolean>} - A promise that resolves when the bucket is deleted.
* @example
* // Delete bucket with name of `bucket-name-to-delete`
* await bucketManager.delete(`bucket-name-to-delete`);
*/
async delete(name) {
const command = new DeleteBucketCommand({
Bucket: name,
});
await this.#client.send(command);
return true;
}
/**
* @summary Sets the privacy of a given bucket.
* @param {string} name - The name of the bucket to toggle.
* @param {boolean} targetState - The new target state. [true=private,false=public]
* @returns {Promise<boolean>} A promise that resolves to true if the bucket was successfully toggled.
* @example
* // Toggle bucket with label of `toggle-bucket-example`
* await bucketManager.setPrivacy(`toggle-bucket-example`, true); // Enabled
* await bucketManager.setPrivacy(`toggle-bucket-example`, false); // Disabled
*/
async setPrivacy(name, targetState) {
const command = new PutBucketAclCommand({
Bucket: name,
ACL: targetState ? "private" : "public-read",
});
await this.#client.send(command);
return true;
}
/**
* @summary Gets the privacy of a given bucket
* @param {string} name - The name of the bucket to query.
* @returns {Promise<boolean>} A promise that resolves to true if the bucket is private.
*/
async getPrivacy(name) {
const command = new GetBucketAclCommand({
Bucket: name,
});
const response = await this.#client.send(command),
readPermission = response.Grants.find((grant) => {
return grant.Grantee.Type === "Group" && grant.Permission === "READ";
});
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;