oak-external-sdk/lib/service/tencent/Tencent.js

150 lines
4.4 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"));
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;
}
}
}
exports.TencentYunInstance = TencentYunInstance;