-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.mjs
91 lines (73 loc) · 1.93 KB
/
uploader.mjs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
CreateMultipartUploadCommand,
UploadPartCommand,
CompleteMultipartUploadCommand,
AbortMultipartUploadCommand
} from '@aws-sdk/client-s3'
import { s3 } from './client.mjs'
export class S3MultiPart {
static get s3Client () {
if (!this._s3Client) {
// this._s3Client = new S3Client({})
this._s3Client = s3.client
}
return this._s3Client
}
constructor (bucketName, key, s3Client = S3MultiPart.s3Client) {
this.s3Client = s3Client
this.bucketName = bucketName
this.key = key
}
async createMultipartUpload () {
const command = new CreateMultipartUploadCommand({
Bucket: this.bucketName,
Key: this.key
})
const mpUpload = await this.s3Client.send(command)
this.uploadId = mpUpload.UploadId
return mpUpload
}
async uploadPart (PartNumber, Body) {
if (this.uploadId == null) {
try {
await this.createMultipartUpload()
} catch (e) {
console.error(e)
throw e
}
}
const command = new UploadPartCommand({
Bucket: this.bucketName,
Key: this.key,
UploadId: this.uploadId,
Body,
PartNumber
})
return this.s3Client.send(command)
}
async completeMultipartUpload (Parts) {
const command = new CompleteMultipartUploadCommand({
Bucket: this.bucketName,
Key: this.key,
UploadId: this.uploadId,
MultipartUpload: { Parts }
})
return this.s3Client.send(command)
// const completeCommand = new CompleteMultipartUploadCommand({
// Bucket: bucketName,
// Key: key,
// UploadId,
// MultipartUpload: { Parts }
// })
// await S3MultiPart.s3Client.send(completeCommand)
}
async abortMultipartUpload () {
if (!this.uploadId) return
const command = new AbortMultipartUploadCommand({
Bucket: this.bucketName,
Key: this.key,
UploadId: this.uploadId
})
return this.s3Client.send(command)
}
}