import OSS from 'ali-oss'; function getEndpoint(RegionID) { return `oss-${RegionID}.aliyuncs.com`; } const ALiYun_ENDPOINT_LIST = { 'cn-hangzhou': { ul: getEndpoint('cn-hangzhou'), }, 'cn-shanghai': { ul: getEndpoint('cn-shanghai'), }, 'cn-nanjing': { ul: getEndpoint('cn-nanjing'), }, 'cn-fuzhou': { ul: getEndpoint('cn-fuzhou'), }, 'cn-wuhan': { ul: getEndpoint('cn-wuhan'), }, 'cn-qingdao': { ul: getEndpoint('cn-qingdao'), }, 'cn-beijing': { ul: getEndpoint('cn-beijing'), }, 'cn-zhangjiakou': { ul: getEndpoint('cn-zhangjiakou'), }, 'cn-huhehaote': { ul: getEndpoint('cn-huhehaote'), }, 'cn-wulanchabu': { ul: getEndpoint('cn-wulanchabu'), }, 'cn-shenzhen': { ul: getEndpoint('cn-shenzhen'), }, 'cn-heyuan': { ul: getEndpoint('cn-heyuan'), }, 'cn-guangzhou': { ul: getEndpoint('cn-guangzhou'), }, 'cn-chengdu': { ul: getEndpoint('cn-chengdu'), }, 'cn-hongkong': { ul: getEndpoint('cn-hongkong'), }, 'us-west-1': { ul: getEndpoint('us-west-1'), }, 'us-east-1': { ul: getEndpoint('us-east-1'), }, 'ap-northeast-1': { ul: getEndpoint('ap-northeast-1'), }, 'ap-northeast-2': { ul: getEndpoint('ap-northeast-2'), }, 'ap-southeast-1': { ul: getEndpoint('ap-southeast-1'), }, 'ap-southeast-2': { ul: getEndpoint('ap-southeast-2'), }, 'ap-southeast-3': { ul: getEndpoint('ap-southeast-3'), }, 'ap-southeast-5': { ul: getEndpoint('ap-southeast-5'), }, 'ap-southeast-6': { ul: getEndpoint('ap-southeast-6'), }, 'ap-southeast-7': { ul: getEndpoint('ap-southeast-7'), }, 'ap-south-1': { ul: getEndpoint('ap-south-1'), }, 'eu-central-1': { ul: getEndpoint('eu-central-1'), }, 'eu-west-1': { ul: getEndpoint('eu-west-1'), }, 'me-east-1': { ul: getEndpoint('me-east-1'), }, 'rg-china-mainland': { ul: getEndpoint('rg-china-mainland'), }, }; export class ALiYunInstance { accessKey; secretKey; constructor(accessKey, secretKey) { this.accessKey = accessKey; this.secretKey = secretKey; } getUploadInfo(bucket, zone, key) { try { const signInfo = this.getSignInfo(bucket); return { key, accessKey: this.accessKey, policy: signInfo.policy, signature: signInfo.signature, uploadHost: `https://${bucket}.${ALiYun_ENDPOINT_LIST[zone].ul}`, bucket, }; } catch (err) { throw err; } } //https://help.aliyun.com/zh/oss/user-guide/form-upload?spm=a2c4g.11186623.0.0.22147f6b1UaGxF#6ee9b6b0be6on getSignInfo(bucket) { const client = new OSS({ accessKeyId: this.accessKey, accessKeySecret: this.secretKey, bucket: bucket, }); const now = new Date(); now.setDate(now.getDate() + 1); const policy = { expiration: now.toISOString(), conditions: [ // 设置上传文件的大小限制。 ['content-length-range', 0, 1048576000], // 限制可上传的Bucket。 { bucket: bucket }, ], }; const data = client.calculatePostSignature(policy); return { policy: data.policy, signature: data.Signature, ossAccessKeyId: data.OSSAccessKeyId, }; } async removeFile(srcBucket, zone, srcKey) { const client = new OSS({ // oss-cn-hangzhou填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。 region: `oss-${zone}`, accessKeyId: this.accessKey, accessKeySecret: this.secretKey, // 填写Bucket名称。 bucket: srcBucket, }); try { // 填写Object完整路径。Object完整路径中不能包含Bucket名称。 const result = await client.delete(srcKey); return result; } catch (error) { throw error; } } async isExistObject(srcBucket, zone, srcKey) { const client = new OSS({ // yourregion填写Bucket所在地域。以华东1(杭州)为例,Region填写为oss-cn-hangzhou。 region: `oss-${zone}`, accessKeyId: this.accessKey, accessKeySecret: this.secretKey, bucket: srcBucket, }); let result; try { result = await client.head(srcKey); return true; } catch (error) { if (error.code === 'NoSuchKey') { return false; } throw error; } } }