新增支持s3协议对象存储
This commit is contained in:
54
mail-worker/src/service/s3-service.js
Normal file
54
mail-worker/src/service/s3-service.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import { S3Client, PutObjectCommand, DeleteObjectsCommand } from "@aws-sdk/client-s3";
|
||||
import settingService from './setting-service';
|
||||
import domainUtils from '../utils/domain-uitls';
|
||||
const s3Service = {
|
||||
|
||||
async putObj(c, key, content, metadata) {
|
||||
|
||||
const client = await this.client(c);
|
||||
|
||||
const { bucket } = await settingService.query(c);
|
||||
|
||||
await client.send(
|
||||
new PutObjectCommand({ Bucket: bucket, Key: key, Body: content, ...metadata })
|
||||
)
|
||||
},
|
||||
|
||||
async deleteObj(c,keys) {
|
||||
|
||||
if (typeof keys === 'string') {
|
||||
keys = [keys]
|
||||
}
|
||||
|
||||
if (keys.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const client = await this.client(c)
|
||||
|
||||
const { bucket } = await settingService.query(c);
|
||||
|
||||
await client.send(
|
||||
new DeleteObjectsCommand({
|
||||
Bucket: bucket,
|
||||
Delete: {
|
||||
Objects: keys.map(key => ({Key: key}))
|
||||
}
|
||||
})
|
||||
)
|
||||
},
|
||||
|
||||
async client(c) {
|
||||
const { region, endpoint, s3AccessKey, s3SecretKey } = await settingService.query(c);
|
||||
return new S3Client({
|
||||
region: region,
|
||||
endpoint: domainUtils.toOssDomain(endpoint),
|
||||
credentials: {
|
||||
accessKeyId: s3AccessKey,
|
||||
secretAccessKey: s3SecretKey,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default s3Service
|
||||
Reference in New Issue
Block a user