mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-07-10 06:52:24 +08:00
✨ Feature(custom): support create bucket for s3
This commit is contained in:
@@ -12,7 +12,10 @@ import {
|
||||
DeleteObjectCommand,
|
||||
DeleteObjectsCommand,
|
||||
PutObjectCommand,
|
||||
S3ClientConfig
|
||||
S3ClientConfig,
|
||||
CreateBucketCommand,
|
||||
PutPublicAccessBlockCommand,
|
||||
PutBucketAclCommand
|
||||
} from '@aws-sdk/client-s3'
|
||||
|
||||
// AWS S3 上传和进度
|
||||
@@ -171,6 +174,89 @@ class S3plistApi {
|
||||
}
|
||||
}
|
||||
|
||||
async putPublicAccess (bucketName: string, client: S3Client) {
|
||||
const input = {
|
||||
Bucket: bucketName,
|
||||
PublicAccessBlockConfiguration: {
|
||||
BlockPublicAcls: false,
|
||||
IgnorePublicAcls: false,
|
||||
BlockPublicPolicy: false,
|
||||
RestrictPublicBuckets: false
|
||||
}
|
||||
}
|
||||
const command = new PutPublicAccessBlockCommand(input)
|
||||
const data = await client.send(command)
|
||||
if (data.$metadata.httpStatusCode !== 200) {
|
||||
this.logParam(data, 'putPublicAccess')
|
||||
throw new Error('manage.setting.putPublicAccessError')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新建存储桶
|
||||
* @param {Object} configMap
|
||||
* configMap = {
|
||||
* BucketName: string,
|
||||
* region: string,
|
||||
* acl: string
|
||||
* }
|
||||
*/
|
||||
async createBucket (configMap: IStringKeyMap): Promise<boolean> {
|
||||
const { BucketName, region, acl, endpoint } = configMap
|
||||
try {
|
||||
await this.getDogeCloudToken()
|
||||
const options = Object.assign({}, this.baseOptions) as S3ClientConfig
|
||||
options.region = String(region) || 'us-east-1'
|
||||
const client = new S3Client(options)
|
||||
const command = new ListBucketsCommand({})
|
||||
const data = await client.send(command)
|
||||
if (data.$metadata.httpStatusCode === 200) {
|
||||
const bucketList = data.Buckets?.map((item) => item.Name)
|
||||
if (bucketList?.includes(BucketName)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if (endpoint === '' || endpoint.includes('amazonaws')) {
|
||||
const createCommand = new CreateBucketCommand({
|
||||
Bucket: BucketName,
|
||||
ObjectOwnership: 'BucketOwnerPreferred'
|
||||
})
|
||||
const createData = await client.send(createCommand)
|
||||
if (createData.$metadata.httpStatusCode === 200) {
|
||||
if (acl !== 'private') {
|
||||
await this.putPublicAccess(BucketName, client)
|
||||
const putACLCommand = new PutBucketAclCommand({
|
||||
Bucket: BucketName,
|
||||
ACL: acl
|
||||
})
|
||||
const putACLData = await client.send(putACLCommand)
|
||||
if (putACLData.$metadata.httpStatusCode !== 200) {
|
||||
this.logParam(putACLData, 'createBucket')
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
this.logParam(createData, 'createBucket')
|
||||
}
|
||||
} else {
|
||||
const createCommand = new CreateBucketCommand({
|
||||
Bucket: BucketName,
|
||||
ACL: acl
|
||||
})
|
||||
const createData = await client.send(createCommand)
|
||||
if (createData.$metadata.httpStatusCode === 200) {
|
||||
return true
|
||||
} else {
|
||||
this.logParam(createData, 'createBucket')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
this.logParam(error, 'createBucket')
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储桶列表
|
||||
*/
|
||||
|
||||
@@ -108,13 +108,8 @@ class TcyunApi {
|
||||
* acl: private | publicRead | publicReadWrite
|
||||
*/
|
||||
async createBucket (configMap: IStringKeyMap): Promise < boolean > {
|
||||
const aclTransMap: IStringKeyMap = {
|
||||
private: 'private',
|
||||
publicRead: 'public-read',
|
||||
publicReadWrite: 'public-read-write'
|
||||
}
|
||||
const res = await this.ctx.putBucket({
|
||||
ACL: aclTransMap[configMap.acl],
|
||||
ACL: configMap.acl,
|
||||
Bucket: configMap.BucketName,
|
||||
Region: configMap.region
|
||||
})
|
||||
|
||||
@@ -230,6 +230,7 @@ export class ManageApi extends EventEmitter implements ManageApiType {
|
||||
case 'tcyun':
|
||||
case 'aliyun':
|
||||
case 'qiniu':
|
||||
case 's3plist':
|
||||
try {
|
||||
client = this.createClient() as any
|
||||
return await client.createBucket(param!)
|
||||
|
||||
Reference in New Issue
Block a user