-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·37 lines (28 loc) · 1.04 KB
/
index.ts
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
import * as AWS from 'aws-sdk';
import * as s3Zip from 's3-zip';
export const handler = async function (event, context) {
try {
const region = process.env.region;
const bucket = process.env.bucket;
const srcFolderName = process.env.srcFolderName;
const destFolderName = process.env.destFolderName;
const destFileName = process.env.fileName;
const srcFolderPath = event.srcFolderPath;
const s3 = new AWS.S3({region: region});
const files = await s3.listObjects({Bucket: bucket, Prefix: srcFolderPath}).promise();
const fileNames = files.Contents
.map(item => item.Key.split('/').pop())
.filter(name => !!name);
const stream = s3Zip.archive({region: region, bucket: bucket}, srcFolderPath, fileNames);
const destKey = srcFolderPath.replace(srcFolderName, destFolderName) + destFileName;
await s3.upload({
Bucket: bucket,
Key: destKey,
Body: stream,
}).promise();
} catch (e) {
const err = 'catched error: ' + e;
console.log(err);
context.fail(err);
}
};