183 lines
5.6 KiB
JavaScript
183 lines
5.6 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.TencentYunInstance = void 0;
|
||
const tslib_1 = require("tslib");
|
||
const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
||
const Exception_1 = require("oak-domain/lib/types/Exception");
|
||
function getEndpoint(RegionID) {
|
||
return `cos-${RegionID}.myqcloud.com`;
|
||
}
|
||
const Tencent_ENDPOINT_LIST = {
|
||
'ap-beijing': {
|
||
ul: getEndpoint('ap-nanjing'),
|
||
},
|
||
'ap-nanjing': {
|
||
ul: getEndpoint('ap-nanjing'),
|
||
},
|
||
'ap-shanghai': {
|
||
ul: getEndpoint('ap-shanghai'),
|
||
},
|
||
'ap-guangzhou': {
|
||
ul: getEndpoint('ap-guangzhou'),
|
||
},
|
||
'ap-chengdu': {
|
||
ul: getEndpoint('ap-chengdu'),
|
||
},
|
||
'ap-chongqing': {
|
||
ul: getEndpoint('ap-chongqing'),
|
||
},
|
||
'ap-shenzhen-fsi': {
|
||
ul: getEndpoint('ap-shenzhen-fsi'),
|
||
},
|
||
'ap-shanghai-fsi': {
|
||
ul: getEndpoint('ap-shanghai-fsi'),
|
||
},
|
||
'ap-beijing-fsi': {
|
||
ul: getEndpoint('ap-beijing-fsi'),
|
||
},
|
||
'ap-hongkong': {
|
||
ul: getEndpoint('ap-hongkong'),
|
||
},
|
||
'ap-singapore': {
|
||
ul: getEndpoint('ap-singapore'),
|
||
},
|
||
};
|
||
// TODO 腾讯云代码未验证
|
||
class TencentYunInstance {
|
||
accessKey;
|
||
secretKey;
|
||
COS;
|
||
constructor(accessKey, secretKey) {
|
||
this.accessKey = accessKey;
|
||
this.secretKey = secretKey;
|
||
this.COS = require('cos-wx-sdk-v5');
|
||
}
|
||
getUploadInfo(bucket, zone, key) {
|
||
try {
|
||
const signInfo = this.getSignInfo(bucket);
|
||
return {
|
||
key,
|
||
accessKey: this.accessKey,
|
||
policy: signInfo.policy,
|
||
signature: signInfo.signature,
|
||
uploadHost: `https://${bucket}.${Tencent_ENDPOINT_LIST[zone].ul}`,
|
||
bucket,
|
||
keyTime: signInfo.keyTime,
|
||
algorithm: signInfo.algorithm,
|
||
};
|
||
}
|
||
catch (err) {
|
||
throw err;
|
||
}
|
||
}
|
||
//https://cloud.tencent.com/document/product/436/7778
|
||
getSignInfo(bucket) {
|
||
const expiration = new Date();
|
||
expiration.setDate(expiration.getDate() + 1);
|
||
const keyTime = `${Math.floor(Date.now() / 1000)};${Math.floor(expiration.getTime() / 1000)}`;
|
||
const algorithm = 'sha1';
|
||
const policy = {
|
||
expiration: expiration.toISOString(),
|
||
conditions: [
|
||
// 限制可上传的Bucket。
|
||
{ bucket: bucket },
|
||
['eq', '$x-cos-server-side-encryption', 'AES256'],
|
||
{ 'q-sign-algorithm': algorithm },
|
||
{ 'q-ak': this.accessKey },
|
||
{ 'q-sign-time': keyTime },
|
||
],
|
||
};
|
||
const encodedPolicy = Buffer.from(JSON.stringify(policy)).toString('base64');
|
||
const signKey = crypto_1.default
|
||
.createHmac('sha1', this.secretKey)
|
||
.update(keyTime)
|
||
.digest();
|
||
const stringToSign = crypto_1.default
|
||
.createHash('sha1')
|
||
.update(encodedPolicy)
|
||
.digest('hex');
|
||
const signature = crypto_1.default
|
||
.createHmac('sha1', signKey)
|
||
.update(stringToSign)
|
||
.digest('hex');
|
||
return {
|
||
policy: encodedPolicy,
|
||
signature,
|
||
keyTime,
|
||
algorithm,
|
||
};
|
||
}
|
||
async removeFile(srcBucket, zone, srcKey) {
|
||
const client = new this.COS({
|
||
SecretId: this.accessKey,
|
||
SecretKey: this.secretKey,
|
||
});
|
||
try {
|
||
// 填写Object完整路径。Object完整路径中不能包含Bucket名称。
|
||
const result = await client.deleteObject({
|
||
Bucket: srcBucket,
|
||
Region: zone,
|
||
Key: srcKey,
|
||
});
|
||
return result;
|
||
}
|
||
catch (error) {
|
||
throw error;
|
||
}
|
||
}
|
||
async isExistObject(srcBucket, zone, srcKey) {
|
||
const client = new this.COS({
|
||
SecretId: this.accessKey,
|
||
SecretKey: this.secretKey,
|
||
});
|
||
let result;
|
||
try {
|
||
result = await client.headObject({
|
||
Bucket: srcBucket,
|
||
Region: zone,
|
||
Key: srcKey,
|
||
});
|
||
return true;
|
||
}
|
||
catch (error) {
|
||
if (error.code === 'NoSuchKey') {
|
||
return false;
|
||
}
|
||
throw error;
|
||
}
|
||
}
|
||
/**
|
||
* 获取预签名对象URL(统一接口)
|
||
* 生成腾讯云对象的预签名访问URL
|
||
*/
|
||
async presignObjectUrl(method, bucket, zone, key, options) {
|
||
const client = new this.COS({
|
||
SecretId: this.accessKey,
|
||
SecretKey: this.secretKey,
|
||
});
|
||
try {
|
||
return new Promise((resolve, reject) => {
|
||
client.getObjectUrl({
|
||
Bucket: bucket,
|
||
Region: zone,
|
||
Key: key,
|
||
Method: method,
|
||
Expires: options?.expires || 3600,
|
||
Sign: true,
|
||
}, (err, data) => {
|
||
if (err) {
|
||
reject(new Exception_1.OakExternalException('tencent', err.code, err.message, err, 'oak-external-sdk', {}));
|
||
}
|
||
else {
|
||
resolve({ url: data.Url });
|
||
}
|
||
});
|
||
});
|
||
}
|
||
catch (error) {
|
||
throw new Exception_1.OakExternalException('tencent', error.code, error.message, error, 'oak-external-sdk', {});
|
||
}
|
||
}
|
||
}
|
||
exports.TencentYunInstance = TencentYunInstance;
|