-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3.js
More file actions
64 lines (55 loc) · 1.91 KB
/
Copy paths3.js
File metadata and controls
64 lines (55 loc) · 1.91 KB
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
const {env} = require('process');
const {S3} = require('@aws-sdk/client-s3');
const {Upload} = require('@aws-sdk/lib-storage');
const {createObjectReadable, accumulateObjects} = require('./stream.js');
const E = module.exports;
const resolveOpt = (opt={})=>({
...opt,
client: opt.client||E.createClient(),
bucket: opt.bucket||env.BUCKETEER_BUCKET_NAME,
});
E.createClient = ({region, accessKeyId, secretAccessKey}={})=>new S3({
region: region||env.BUCKETEER_AWS_REGION,
credentials: {
accessKeyId: accessKeyId||env.BUCKETEER_AWS_ACCESS_KEY_ID,
secretAccessKey: secretAccessKey||env.BUCKETEER_AWS_SECRET_ACCESS_KEY,
},
});
E.upload = (filepath, dataStream, opt={})=>{
const {client, bucket} = resolveOpt(opt);
return new Upload({client, params: {
Bucket: bucket,
Key: filepath,
Body: dataStream,
}}).done();
};
E.download = async (filepath, opt={})=>{
const {client, bucket} = resolveOpt(opt);
return (await client.getObject({
Bucket: bucket,
Key: filepath,
})).Body.transformToWebStream();
};
E.listObjects = opt=>accumulateObjects(E.streamObjects(opt));
E.streamObjects = (opt={})=>{
const {client, bucket} = resolveOpt(opt);
let continuationToken;
return createObjectReadable(async push=>{
const data = await client.listObjectsV2({
Bucket: bucket,
...continuationToken&&{ContinuationToken: continuationToken},
});
data.Contents.map(c=>c.Key).forEach(k=>push(k));
if (!data.IsTruncated)
return void push(null);
continuationToken = data.NextContinuationToken;
});
};
E.deleteObject = (filepath, opt={})=>{
const {client, bucket} = resolveOpt(opt);
return client.deleteObject({
Bucket: bucket,
Key: filepath,
});
};
// TODO: https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-s3/Class/S3Client/