-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
74 lines (61 loc) · 1.59 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
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
import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3';
import { Upload } from '@aws-sdk/lib-storage';
import * as archiver from 'archiver';
import * as path from 'path';
import * as stream from 'stream';
import { pipeline } from 'stream';
import {
AWS_ACCESS_KEY_ID,
AWS_BUCKET,
AWS_REGION,
AWS_SECRET_ACCESS_KEY,
} from './config';
const s3Client = new S3Client([
{
region: AWS_REGION,
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
},
},
]);
const targetDirectory = path.join(__dirname, 'images/targets');
const main = async () => {
const archive = archiver('zip', { zlib: { level: 9 } });
const { PassThrough } = stream;
const passThrough = new PassThrough();
archive.on('error', (err) => {
console.log('archive error', err);
throw err;
});
archive.directory(targetDirectory, false);
// pipeline 을 통해 archive 를 passThrough 로 전달, 읽기 스트림 으로 변환
pipeline(archive, passThrough, (err) => {
if (err) {
console.log('pipeline error', err);
throw err;
}
});
archive.finalize().then(() => {
passThrough.end();
});
const uploadedName = `images.zip`;
const params: PutObjectCommandInput = {
Bucket: AWS_BUCKET,
Key: uploadedName,
Body: passThrough,
};
const uploader = new Upload({
client: s3Client,
params,
});
uploader.on('httpUploadProgress', (progress) => {
console.log('upload progress', progress);
});
await uploader.done();
};
main()
.then(() => {
console.log('done');
})
.catch(console.error);