-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy paths3.js
44 lines (35 loc) · 926 Bytes
/
s3.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
import AWS from 'aws-sdk'; // eslint-disable-line import/no-extraneous-dependencies
export default class Storage {
constructor({ region, bucket }) {
this.S3 = new AWS.S3({
signatureVersion: 'v4',
region,
params: {
Bucket: bucket,
},
});
}
async put(key, data, encoding) {
return this.S3.putObject({
Key: key,
Body: encoding === 'base64' ? new Buffer(data, 'base64') : data,
}).promise();
}
async get(key) {
const meta = await this.S3.getObject({
Key: key,
}).promise();
return meta.Body;
}
async listAllKeys(token = null, keys = []) {
const data = await this.S3.listObjectsV2({
ContinuationToken: token,
})
.promise();
keys.push(data.Contents);
if (data.IsTruncated) {
return this.listAllKeys(data.NextContinuationToken, keys);
}
return [].concat(...keys).map(({ Key }) => Key);
}
}