-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
61 lines (46 loc) · 1.73 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
import { PutObjectCommandInput, S3Client } from '@aws-sdk/client-s3'
import { s3 } from './src/s3'
import path from 'path'
import fs from 'fs'
import os from 'os'
const getDirectoryToExtract = (pathToExtract?: string) => {
if (!pathToExtract) {
const tmpPath = fs.mkdtempSync(path.join(os.tmpdir(), 'unzipped-'))
return tmpPath
}
return pathToExtract
}
interface IS3ZipHandlerInput {
s3Client: S3Client
bucket: string
key: string,
params?: Pick<PutObjectCommandInput, 'ACL' | 'ContentDisposition'>
}
const decompressLocal = async (input: IS3ZipHandlerInput, pathToExtract?: string): Promise<string> => {
const { s3Client, bucket, key } = input
const dirToExtract = getDirectoryToExtract(pathToExtract)
const decompressor = s3.createDecompressor({ dirToExtract })
const { zip } = await s3.getZipFile({ bucket, key, s3: s3Client })
const { localPath } = await decompressor(zip)
return localPath
}
interface IDecompressToKeyFolderS3Output {
localPath: string;
uploadedPath?: string;
}
const decompressToKeyFolderS3 = async (input: IS3ZipHandlerInput): Promise<IDecompressToKeyFolderS3Output> => {
const { s3Client, bucket, key, params } = input
const dirToExtract = getDirectoryToExtract()
const dirToUpload = path.dirname(key)
const uploader = s3.createUploader({ s3: s3Client, bucket, key: dirToUpload, params })
const decompressor = s3.createDecompressor({ dirToExtract, uploader })
const { zip } = await s3.getZipFile({ bucket, key, s3: s3Client })
const { localPath, uploadedPath } = await decompressor(zip)
return { localPath, uploadedPath }
}
const s3ZipHandler = {
decompressLocal,
decompressToKeyFolderS3
}
export { IS3ZipHandlerInput, IDecompressToKeyFolderS3Output }
export default s3ZipHandler