Skip to content

Commit

Permalink
feat: save
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyan committed Jul 24, 2024
1 parent 7e3dc8a commit bffe3ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ cos.sync(conf.localPath conf.mime, conf.maxAge || conf.cacheMaxAge || 0, functio

## 历史

### 1.7.0 2024-07-24
- v5支持UseAccelerate参数,控制是否支持开启加速域名,默认为false则不开启,true则开启加速域名

### 1.6.0 2024-05-11
- v5支持skipWhenExist参数,默认值为false则上传所有文件,值为true则跳过已上传的文件。

Expand Down
32 changes: 23 additions & 9 deletions lib/cos-v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ function uploadFile(that , remoteFilePath , filePath){
//上传文件
return new Promise(function(resolve , reject){
var filename = encodeURIComponent(path.basename(remoteFilePath));
const uploadAction = () => {
that.cos.putObject({
var isRetryed = false;
const uploadAction = (cos, tip = '') => {
cos.putObject({
Bucket: that.bucket,
Region: that.region,
Key: remoteFilePath,
Expand All @@ -109,11 +110,16 @@ function uploadFile(that , remoteFilePath , filePath){
var _speed = isNaN(Number(progressData.speed)) ? 0 : Number(progressData.speed);
var speed = (_speed / 1024).toFixed(2);

log('[Uploading ] ' + filePath +' => progress: ' + percent + '%. speed: ' + speed + ' kb/s\n');
log(tip + '[Uploading ] ' + filePath +' => progress: ' + percent + '%. speed: ' + speed + ' kb/s\n');
}
}, function (err, data) {
log('[UploadFile ] uploaded ' + filePath +' is ' + (err ? 'error\n' : 'success.\n'));
return err ? reject(err) : resolve(data);
log(tip + '[UploadFile ] uploaded ' + filePath +' is ' + (err ? 'error\n' : 'success.\n'));
if(err && !isRetryed) {
isRetryed = true;
uploadAction(that.cosAccelerate, '启用加速域名重试上传:');
} else {
return err ? reject(err) : resolve(data);
}
});
}
if(that.skipWhenExist) {
Expand All @@ -125,7 +131,7 @@ function uploadFile(that , remoteFilePath , filePath){
}, function (err, data) {
if (err) {
if(err.statusCode === NoFoundCode) {
uploadAction();
uploadAction(that.cos);
} else {
return reject(err)
}
Expand All @@ -138,7 +144,7 @@ function uploadFile(that , remoteFilePath , filePath){
}
});
} else {
uploadAction();
uploadAction(that.cos);
}

});
Expand Down Expand Up @@ -200,11 +206,19 @@ function CosV5(config){
if ( !config.remotePath || !config.bucket || !config.region ) {
return log('[CosV5 ] params remotePath|| bucket || region is undefined.\n');
}
//cos
this.cos = new COS({
const cosConfig = {
SecretId: config.secretId,
SecretKey: config.secretKey,
Timeout: config.timeout || 120000
}
//cos
this.cos = new COS({
...cosConfig
});
// https://cloud.tencent.com/document/product/436/55438#.E5.85.A8.E7.90.83.E5.8A.A0.E9.80.9F.E5.9F.9F.E5.90.8D
this.cosAccelerate = new COS({
...cosConfig,
UseAccelerate: true, // 指定 true,使用全球加速域名请求
});
this.skipWhenExist = config.skipWhenExist ? config.skipWhenExist : false;
this.region = config.region;
Expand Down

0 comments on commit bffe3ac

Please sign in to comment.