1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
Files
epp/frontend/src/utils/qcloud-cos-upload.js
2023-04-04 01:06:22 +08:00

55 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import COS from 'cos-js-sdk-v5';
import * as qCloudCosApi from '../api/qcloud-cos';
export function upload(file) {
console.log("file", file);
return new Promise((resolve, reject) => {
qCloudCosApi.getTmpCosCredential({
ext: 'jpg'
}).then((data) => {
console.log("data", data)
// {
// "tmpSecretId": "",
// "tmpSecretKey": "",
// "sessionToken": "",
// "objectKey": "",
// "startTimestamp": 1680534188320,
// "expiredTimestamp": 1680535988320,
// "bucket": "epp-1302260381",
// "region": "ap-shanghai"
// }
let cos = new COS({
// getAuthorization 必选参数
getAuthorization: function (options, callback) {
// 异步获取临时密钥
callback({
TmpSecretId: data.tmpSecretId,
TmpSecretKey: data.tmpSecretKey,
SecurityToken: data.sessionToken,
// 建议返回服务器时间作为签名的开始时间,避免用户浏览器本地时间偏差过大导致签名错误
StartTime: Math.floor(data.startTimestamp / 1000), // 时间戳单位秒1580000000
ExpiredTime: Math.floor(data.expiredTimestamp / 1000), // 时间戳单位秒1580000000
});
}
});
console.log("cos", cos)
cos.putObject({
Bucket: data.bucket, /* 填入您自己的存储桶,必须字段 */
Region: data.region, /* 存储桶所在地域例如ap-beijing必须字段 */
Key: data.objectKey, /* 存储在桶里的对象键例如1.jpga/b/test.txt必须字段 */
Body: file,
}, function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}).catch((err) => {
reject(err)
})
})
};