-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy paths3.ts
207 lines (187 loc) · 5.06 KB
/
s3.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { ObjectOptions, StoreProvider, StoreProviderConfig } from './base'
import { toBuffer, toStr } from 'libs/shared/str'
import {
CopyObjectCommand,
DeleteObjectCommand,
GetObjectCommand,
HeadObjectCommand,
PutObjectCommand,
S3Client,
} from '@aws-sdk/client-s3'
import { getSignedUrl } from '@aws-sdk/s3-request-presigner'
import { streamToBuffer } from '../utils'
import { Readable } from 'stream'
import { isEmpty, toNumber } from 'lodash'
import { Client as MinioClient } from 'minio'
function isNoSuchKey(err: any) {
return err.code === 'NoSuchKey' || err.message === 'NoSuchKey'
}
/**
* @todo unit test
*/
export interface S3Config extends StoreProviderConfig {
bucket: string
accessKey: string
secretKey: string
endPoint?: string
pathStyle?: boolean
region?: string
}
export class StoreS3 extends StoreProvider {
client: S3Client
config: S3Config
constructor(config: S3Config) {
super(config)
this.client = new S3Client({
forcePathStyle: config.pathStyle,
region: config.region,
endpoint: config.endPoint,
credentials: {
accessKeyId: config.accessKey,
secretAccessKey: config.secretKey,
},
})
this.config = config
}
/**
* FIXME 签名错误在 MinIO 包含端口号时,这里先用 MinioSDK 代替
* @see https://github.com/aws/aws-sdk-js-v3/issues/2121
*/
getSignUrl(path: string, expires = 600) {
if (this.config.endPoint) {
const url = new URL(this.config.endPoint)
if (url.port) {
const client = new MinioClient({
accessKey: this.config.accessKey,
secretKey: this.config.secretKey,
endPoint: url.hostname,
useSSL: url.protocol === 'https:',
port: toNumber(url.port),
})
return client.presignedGetObject(this.config.bucket, this.getPath(path))
}
}
return getSignedUrl(
this.client,
new GetObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
}),
{ expiresIn: expires }
)
}
async hasObject(path: string) {
try {
const data = await this.client.send(
new HeadObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
})
)
return !!data
} catch (e) {
return false
}
}
async getObject(path: string, isCompressed = false) {
let content
try {
const result = await this.client.send(
new GetObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
})
)
content = await streamToBuffer(result.Body as Readable)
} catch (err) {
if (!isNoSuchKey(err)) {
throw err
}
}
return toStr(content, isCompressed)
}
async getObjectMeta(path: string) {
try {
const result = await this.client.send(
new HeadObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
})
)
return result.Metadata
} catch (err) {
if (!isNoSuchKey(err)) {
throw err
}
return
}
}
async getObjectAndMeta(path: string, isCompressed = false) {
let content
let meta
let contentType
try {
const result = await this.client.send(
new GetObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
})
)
content = await streamToBuffer(result.Body as Readable)
meta = result.Metadata
contentType = result.ContentType
} catch (err) {
if (!isNoSuchKey(err)) {
throw err
}
}
return {
content: toStr(content, isCompressed),
meta,
contentType,
buffer: content,
}
}
async putObject(
path: string,
raw: string | Buffer,
options?: ObjectOptions,
isCompressed?: boolean
) {
await this.client.send(
new PutObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
Body: Buffer.isBuffer(raw) ? raw : toBuffer(raw, isCompressed),
Metadata: options?.meta,
CacheControl: options?.headers?.cacheControl,
ContentDisposition: options?.headers?.contentDisposition,
ContentEncoding: options?.headers?.contentEncoding,
ContentType: options?.contentType,
})
)
}
async deleteObject(path: string) {
await this.client.send(
new DeleteObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(path),
})
)
}
async copyObject(fromPath: string, toPath: string, options: ObjectOptions) {
await this.client.send(
new CopyObjectCommand({
Bucket: this.config.bucket,
Key: this.getPath(toPath),
CopySource: `${this.config.bucket}/${this.getPath(fromPath)}`,
Metadata: options?.meta,
CacheControl: options?.headers?.cacheControl,
ContentDisposition: options?.headers?.contentDisposition,
ContentEncoding: options?.headers?.contentEncoding,
ContentType: options?.contentType,
MetadataDirective: isEmpty(options?.meta) ? 'COPY' : 'REPLACE',
})
)
}
}