Feature(custom): support create bucket for s3

This commit is contained in:
Kuingsmile
2024-06-02 11:45:40 +08:00
parent e67313eebb
commit 226f1704c3
9 changed files with 156 additions and 104 deletions

View File

@@ -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
}
/**
* 获取存储桶列表
*/

View File

@@ -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
})

View File

@@ -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!)

View File

@@ -355,7 +355,7 @@ const urlMap : IStringKeyMap = {
webdavplist: 'https://baike.baidu.com/item/WebDAV/4610909'
}
const showNewIconList = ['aliyun', 'qiniu', 'tcyun']
const showNewIconList = ['aliyun', 'qiniu', 'tcyun', 's3plist']
const bucketT = $T('MANAGE_MAIN_PAGE_BUCKET')
const galleryT = $T('MANAGE_MAIN_PAGE_GALLERY')
@@ -415,6 +415,7 @@ function createNewBucket (picBedName: string) {
if (currentPicBedName.value === 'tcyun') {
resultMap.BucketName = `${resultMap.BucketName}-${currentPagePicBedConfig.appId}`
}
resultMap.endpoint = currentPagePicBedConfig.endpoint
invokeToMain('createBucket', currentAlias, resultMap).then((result: any) => {
if (result) {
ElNotification({

View File

@@ -50,8 +50,8 @@ export const newBucketConfig:IStringKeyMap = {
default: 'private',
options: {
private: $T('MANAGE_NEW_BUCKET_TCYUN_ACL_PRIVATE'),
publicRead: $T('MANAGE_NEW_BUCKET_TCYUN_ACL_PUBLIC_R'),
publicReadWrite: $T('MANAGE_NEW_BUCKET_TCYUN_ACL_PUBLIC_RW')
'public-read': $T('MANAGE_NEW_BUCKET_TCYUN_ACL_PUBLIC_R'),
'public-read-write': $T('MANAGE_NEW_BUCKET_TCYUN_ACL_PUBLIC_RW')
}
}
},
@@ -162,67 +162,46 @@ export const newBucketConfig:IStringKeyMap = {
},
options: ['BucketName', 'region', 'acl']
},
upyun: {
name: $T('MANAGE_NEW_BUCKET_UPYUN_NAME'),
icon: 'upyun',
s3plist: {
name: $T('MANAGE_NEW_BUCKET_S3PLIST_NAME'),
icon: 's3plist',
configOptions: {
BucketName: {
required: true,
description: $T('MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_DESC'),
placeholder: $T('MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_PLACEHOLDER'),
description: $T('MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_DESC'),
placeholder: $T('MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_PLACEHOLDER'),
paraType: 'string',
component: 'input',
default: 'piclist',
rule: [
{
required: true,
message: $T('MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_A'),
trigger: 'blur'
},
{
validator: (rule: any, value: any, callback: any) => {
const reg = /^[a-z][a-z0-9-]{4,19}$/
if (value.length > 23 || value.length < 5) {
callback(new Error($T('MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_B')))
} else if (!reg.test(value)) {
callback(new Error($T('MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_C')))
} else {
callback()
}
},
trigger: 'change'
}
]
},
operator: {
required: true,
description: $T('MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_DESC'),
placeholder: $T('MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_PLACEHOLDER'),
paraType: 'string',
component: 'input',
rule: [
{
required: true,
message: $T('MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_RULE_MSG_A'),
message: $T('MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_RULE_MSG_A'),
trigger: 'blur'
}
]
},
password: {
region: {
required: true,
description: $T('MANAGE_NEW_BUCKET_UPYUN_PASSWORD_DESC'),
placeholder: $T('MANAGE_NEW_BUCKET_UPYUN_PASSWORD_PLACEHOLDER'),
description: $T('MANAGE_NEW_BUCKET_S3PLIST_REGION'),
paraType: 'string',
component: 'input',
rule: [
{
required: true,
message: $T('MANAGE_NEW_BUCKET_UPYUN_PASSWORD_RULE_MSG_A'),
trigger: 'blur'
}
]
default: 'us-east-1'
},
acl: {
required: true,
description: $T('MANAGE_NEW_BUCKET_S3PLIST_ACL_DESC'),
paraType: 'string',
component: 'select',
default: 'private',
options: {
private: $T('MANAGE_NEW_BUCKET_S3PLIST_ACL_PRIVATE'),
'public-read': $T('MANAGE_NEW_BUCKET_S3PLIST_ACL_PUBLIC_R'),
'public-read-write': $T('MANAGE_NEW_BUCKET_S3PLIST_ACL_PUBLIC_RW'),
'authenticated-read': $T('MANAGE_NEW_BUCKET_S3PLIST_ACL_AUTHENTICATED_READ')
}
}
},
options: ['BucketName', 'operator', 'password']
options: ['BucketName', 'region', 'acl']
}
}

View File

@@ -901,18 +901,16 @@ interface ILocales {
MANAGE_NEW_BUCKET_QINIU_BUCKETNAME_RULE_MSG_C: string
MANAGE_NEW_BUCKET_QINIU_REGION: string
MANAGE_NEW_BUCKET_QINIU_ACL_DESC: string
MANAGE_NEW_BUCKET_UPYUN_NAME: string
MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_DESC: string
MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_PLACEHOLDER: string
MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_A: string
MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_B: string
MANAGE_NEW_BUCKET_UPYUN_BUCKETNAME_RULE_MSG_C: string
MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_DESC: string
MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_PLACEHOLDER: string
MANAGE_NEW_BUCKET_UPYUN_OPERATORNAME_RULE_MSG_A: string
MANAGE_NEW_BUCKET_UPYUN_PASSWORD_DESC: string
MANAGE_NEW_BUCKET_UPYUN_PASSWORD_PLACEHOLDER: string
MANAGE_NEW_BUCKET_UPYUN_PASSWORD_RULE_MSG_A: string
MANAGE_NEW_BUCKET_S3PLIST_NAME: string
MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_DESC: string
MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_PLACEHOLDER: string
MANAGE_NEW_BUCKET_S3PLIST_BUCKETNAME_RULE_MSG_A: string
MANAGE_NEW_BUCKET_S3PLIST_REGION: string
MANAGE_NEW_BUCKET_S3PLIST_ACL_DESC: string
MANAGE_NEW_BUCKET_S3PLIST_ACL_PUBLIC_RW: string
MANAGE_NEW_BUCKET_S3PLIST_ACL_PUBLIC_R: string
MANAGE_NEW_BUCKET_S3PLIST_ACL_PRIVATE: string
MANAGE_NEW_BUCKET_S3PLIST_ACL_AUTHENTICATED_READ: string
PLUGIN_INSTALL_SUCCEED: string
PLUGIN_INSTALL_FAILED: string
PLUGIN_UNINSTALL_SUCCEED: string