72 lines
3.0 KiB
JavaScript
72 lines
3.0 KiB
JavaScript
|
const COS = require('cos-nodejs-sdk-v5');
|
|||
|
const pathOpt = require("path");
|
|||
|
const cos = new COS({
|
|||
|
SecretId: 'AKIDvjKhqrfEaliRq11nMcrGZmsATiyNl1BA',
|
|||
|
// 推荐使用环境变量获取;用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。
|
|||
|
// 子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140
|
|||
|
SecretKey: 'xpZCjCTVJzZG2wyy8mFVwLWTVVIqAKct',
|
|||
|
Domain: 'https://note-1324909903.cos.ap-beijing.myqcloud.com'
|
|||
|
});
|
|||
|
const Bucket='note-1324909903'
|
|||
|
const Region = 'ap-beijing'
|
|||
|
class UploadUtils {
|
|||
|
constructor(store) {
|
|||
|
this.store = store;
|
|||
|
}
|
|||
|
syncActiveFile() {
|
|||
|
let tableBarItem = JSON.parse(this.store.get("persist:tableBarItem"));
|
|||
|
if (!tableBarItem) {
|
|||
|
return;
|
|||
|
}
|
|||
|
let activeFile = tableBarItem.activeKey?tableBarItem.activeKey.replaceAll('"',""):undefined;
|
|||
|
console.log("activeFile:", activeFile)
|
|||
|
if (activeFile) {
|
|||
|
cos.headObject({
|
|||
|
Bucket:Bucket,
|
|||
|
Region:Region,
|
|||
|
// 不能以 / 开头
|
|||
|
Key: activeFile,
|
|||
|
}, function(err, data) {
|
|||
|
console.log("err || data.CommonPrefixes",err || data);
|
|||
|
if (data&&data.ETag){
|
|||
|
// 文件存在,比较MD5值
|
|||
|
let md5= data.ETag.replaceAll('"',"")
|
|||
|
console.log("fileList[0].ETag",md5)
|
|||
|
}else {
|
|||
|
console.log("cos.uploadFile")
|
|||
|
cos.uploadFile({
|
|||
|
Bucket: Bucket, /* 填入您自己的存储桶,必须字段 */
|
|||
|
Region: Region, /* 存储桶所在地域,例如 ap-beijing,必须字段 */
|
|||
|
Key: activeFile, /* 存储在桶里的对象键(例如1.jpg,a/b/test.txt),必须字段 */
|
|||
|
FilePath: activeFile, /* 必须 */
|
|||
|
SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值,超过5MB使用分块上传,非必须 */
|
|||
|
onTaskReady: function (taskId) { /* 非必须 */
|
|||
|
console.log(taskId);
|
|||
|
},
|
|||
|
onProgress: function (progressData) { /* 非必须 */
|
|||
|
console.log(JSON.stringify(progressData));
|
|||
|
},
|
|||
|
onFileFinish: function (err, data, options) { /* 非必须 */
|
|||
|
console.log(options.Key + '上传' + (err ? '失败' : '完成'));
|
|||
|
},
|
|||
|
// 支持自定义headers 非必须
|
|||
|
}, function (err, data) {
|
|||
|
console.log(err || data);
|
|||
|
});
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
syncFile() {
|
|||
|
console.log("同步数据")
|
|||
|
// 1. 先同步active文件数据
|
|||
|
this.syncActiveFile()
|
|||
|
// 2. 同步bar中数据
|
|||
|
// 3. 同步树中的数据。
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
module.exports = UploadUtils
|