@cloudbase/manager-node 3.9.5 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,18 +134,24 @@ class StorageService {
134
134
  * 上传文件夹
135
135
  * @param {string} localPath 本地文件夹路径
136
136
  * @param {string} cloudPath 云端文件夹
137
+ * @param {number} parallel 并发量
138
+ * @param {number} retryCount 重试次数
139
+ * @param {number} retryInterval 重试时间间隔(毫秒)
137
140
  * @param {(string | string[])} ignore
138
141
  * @param {(string | string[])} ignore
139
142
  * @returns {Promise<void>}
140
143
  */
141
144
  async uploadDirectory(options) {
142
- const { localPath, cloudPath = '', ignore, onProgress, onFileFinish } = options;
145
+ const { localPath, cloudPath = '', ignore, onProgress, onFileFinish, parallel, retryCount, retryInterval } = options;
143
146
  // 此处不检查路径是否存在
144
147
  // 绝对路径 /var/blog/xxxx
145
148
  const { bucket, region } = this.getStorageConfig();
146
149
  return this.uploadDirectoryCustom({
147
150
  localPath,
148
151
  cloudPath,
152
+ parallel,
153
+ retryCount,
154
+ retryInterval,
149
155
  bucket,
150
156
  region,
151
157
  ignore,
@@ -157,13 +163,16 @@ class StorageService {
157
163
  * 上传文件夹,支持自定义 Region 和 Bucket
158
164
  * @param {string} localPath
159
165
  * @param {string} cloudPath
166
+ * @param {number} parallel
167
+ * @param {number} retryCount
168
+ * @param {number} retryInterval
160
169
  * @param {string} bucket
161
170
  * @param {string} region
162
171
  * @param {IOptions} options
163
172
  * @returns {Promise<void>}
164
173
  */
165
174
  async uploadDirectoryCustom(options) {
166
- const { localPath, cloudPath, bucket, region, onProgress, onFileFinish, ignore, fileId = true, parallel = 20 } = options;
175
+ const { localPath, cloudPath, bucket, region, onProgress, onFileFinish, ignore, fileId = true, parallel = 20, retryCount = 0, retryInterval = 500 } = options;
167
176
  // 此处不检查路径是否存在
168
177
  // 绝对路径 /var/blog/xxxx
169
178
  const resolvePath = path_1.default.resolve(localPath);
@@ -229,11 +238,18 @@ class StorageService {
229
238
  // 对文件上传进行处理
230
239
  const cos = this.getCos(parallel);
231
240
  const uploadFiles = util_1.default.promisify(cos.uploadFiles).bind(cos);
232
- return uploadFiles({
241
+ const params = {
233
242
  files,
234
243
  SliceSize: BIG_FILE_SIZE,
235
244
  onProgress,
236
245
  onFileFinish
246
+ };
247
+ return this.uploadFilesWithRetry({
248
+ uploadFiles,
249
+ options: params,
250
+ times: retryCount,
251
+ interval: retryInterval,
252
+ failedFiles: []
237
253
  });
238
254
  }
239
255
  /**
@@ -847,6 +863,42 @@ class StorageService {
847
863
  env: envConfig.EnvId
848
864
  };
849
865
  }
866
+ /**
867
+ * 带重试功能的上传多文件函数
868
+ * @param uploadFiles sdk上传函数
869
+ * @param options sdk上传函数参数
870
+ * @param times 重试次数
871
+ * @param interval 重试时间间隔(毫秒)
872
+ * @param failedFiles 失败文件列表
873
+ * @returns
874
+ */
875
+ async uploadFilesWithRetry({ uploadFiles, options, times, interval, failedFiles }) {
876
+ console.log('times', times);
877
+ const { files, onFileFinish } = options;
878
+ const tempFailedFiles = [];
879
+ const res = await uploadFiles(Object.assign(Object.assign({}, options), { files: failedFiles.length
880
+ ? files.filter(file => failedFiles.includes(file.Key))
881
+ : files, onFileFinish: (...args) => {
882
+ console.log('args', args);
883
+ const error = args[0];
884
+ const fileInfo = args[2];
885
+ if (error) {
886
+ tempFailedFiles.push(fileInfo.Key);
887
+ }
888
+ onFileFinish === null || onFileFinish === void 0 ? void 0 : onFileFinish.apply(null, args);
889
+ } }));
890
+ if (!(tempFailedFiles === null || tempFailedFiles === void 0 ? void 0 : tempFailedFiles.length) || times <= 0)
891
+ return res;
892
+ if (times > 0) {
893
+ setTimeout(() => this.uploadFilesWithRetry({
894
+ uploadFiles,
895
+ options,
896
+ times: times - 1,
897
+ interval,
898
+ failedFiles: tempFailedFiles
899
+ }), interval);
900
+ }
901
+ }
850
902
  }
851
903
  __decorate([
852
904
  utils_1.preLazy()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/manager-node",
3
- "version": "3.9.5",
3
+ "version": "3.10.0",
4
4
  "description": "The node manage service api for cloudbase.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -49,6 +49,12 @@ export interface IFileOptions extends IOptions {
49
49
  localPath: string
50
50
  // cloudPath 可以为空
51
51
  cloudPath?: string
52
+ // 并发数量
53
+ parallel?: number
54
+ // 重试次数
55
+ retryCount?: number
56
+ // 重试时间间隔(毫秒)
57
+ retryInterval?: number
52
58
  }
53
59
 
54
60
  export interface IFilesOptions extends IOptions {
@@ -230,19 +236,34 @@ export class StorageService {
230
236
  * 上传文件夹
231
237
  * @param {string} localPath 本地文件夹路径
232
238
  * @param {string} cloudPath 云端文件夹
239
+ * @param {number} parallel 并发量
240
+ * @param {number} retryCount 重试次数
241
+ * @param {number} retryInterval 重试时间间隔(毫秒)
233
242
  * @param {(string | string[])} ignore
234
243
  * @param {(string | string[])} ignore
235
244
  * @returns {Promise<void>}
236
245
  */
237
246
  @preLazy()
238
247
  public async uploadDirectory(options: IFileOptions): Promise<void> {
239
- const { localPath, cloudPath = '', ignore, onProgress, onFileFinish } = options
248
+ const {
249
+ localPath,
250
+ cloudPath = '',
251
+ ignore,
252
+ onProgress,
253
+ onFileFinish,
254
+ parallel,
255
+ retryCount,
256
+ retryInterval
257
+ } = options
240
258
  // 此处不检查路径是否存在
241
259
  // 绝对路径 /var/blog/xxxx
242
260
  const { bucket, region } = this.getStorageConfig()
243
261
  return this.uploadDirectoryCustom({
244
262
  localPath,
245
263
  cloudPath,
264
+ parallel,
265
+ retryCount,
266
+ retryInterval,
246
267
  bucket,
247
268
  region,
248
269
  ignore,
@@ -255,6 +276,9 @@ export class StorageService {
255
276
  * 上传文件夹,支持自定义 Region 和 Bucket
256
277
  * @param {string} localPath
257
278
  * @param {string} cloudPath
279
+ * @param {number} parallel
280
+ * @param {number} retryCount
281
+ * @param {number} retryInterval
258
282
  * @param {string} bucket
259
283
  * @param {string} region
260
284
  * @param {IOptions} options
@@ -271,7 +295,9 @@ export class StorageService {
271
295
  onFileFinish,
272
296
  ignore,
273
297
  fileId = true,
274
- parallel = 20
298
+ parallel = 20,
299
+ retryCount = 0,
300
+ retryInterval = 500
275
301
  } = options
276
302
  // 此处不检查路径是否存在
277
303
  // 绝对路径 /var/blog/xxxx
@@ -310,12 +336,13 @@ export class StorageService {
310
336
  const creatingDirController = new AsyncTaskParallelController(parallel, 50)
311
337
  const creatingDirTasks = fileStatsList
312
338
  .filter(info => info.isDir)
313
- .map(info => () =>
314
- this.createCloudDirectroyCustom({
315
- cloudPath: info.cloudFileKey,
316
- bucket,
317
- region
318
- })
339
+ .map(
340
+ info => () =>
341
+ this.createCloudDirectroyCustom({
342
+ cloudPath: info.cloudFileKey,
343
+ bucket,
344
+ region
345
+ })
319
346
  )
320
347
 
321
348
  creatingDirController.loadTasks(creatingDirTasks)
@@ -348,15 +375,20 @@ export class StorageService {
348
375
  // 对文件上传进行处理
349
376
  const cos = this.getCos(parallel)
350
377
  const uploadFiles = Util.promisify(cos.uploadFiles).bind(cos)
351
-
352
- return uploadFiles({
378
+ const params = {
353
379
  files,
354
380
  SliceSize: BIG_FILE_SIZE,
355
381
  onProgress,
356
382
  onFileFinish
383
+ }
384
+ return this.uploadFilesWithRetry({
385
+ uploadFiles,
386
+ options: params,
387
+ times: retryCount,
388
+ interval: retryInterval,
389
+ failedFiles: []
357
390
  })
358
391
  }
359
-
360
392
  /**
361
393
  * 批量上传文件
362
394
  * @param options
@@ -735,9 +767,7 @@ export class StorageService {
735
767
  * @returns {Promise<void>}
736
768
  */
737
769
  @preLazy()
738
- public async deleteDirectory(
739
- cloudPath: string
740
- ): Promise<{
770
+ public async deleteDirectory(cloudPath: string): Promise<{
741
771
  Deleted: { Key: string }[]
742
772
  Error: Object[]
743
773
  }> {
@@ -758,9 +788,7 @@ export class StorageService {
758
788
  * @returns {Promise<void>}
759
789
  */
760
790
  @preLazy()
761
- public async deleteDirectoryCustom(
762
- options: { cloudPath: string } & ICustomOptions
763
- ): Promise<{
791
+ public async deleteDirectoryCustom(options: { cloudPath: string } & ICustomOptions): Promise<{
764
792
  Deleted: { Key: string }[]
765
793
  Error: Object[]
766
794
  }> {
@@ -1127,4 +1155,47 @@ export class StorageService {
1127
1155
  env: envConfig.EnvId
1128
1156
  }
1129
1157
  }
1158
+ /**
1159
+ * 带重试功能的上传多文件函数
1160
+ * @param uploadFiles sdk上传函数
1161
+ * @param options sdk上传函数参数
1162
+ * @param times 重试次数
1163
+ * @param interval 重试时间间隔(毫秒)
1164
+ * @param failedFiles 失败文件列表
1165
+ * @returns
1166
+ */
1167
+ private async uploadFilesWithRetry({ uploadFiles, options, times, interval, failedFiles }) {
1168
+ console.log('times', times)
1169
+ const { files, onFileFinish } = options
1170
+ const tempFailedFiles = []
1171
+ const res = await uploadFiles({
1172
+ ...options,
1173
+ files: failedFiles.length
1174
+ ? files.filter(file => failedFiles.includes(file.Key))
1175
+ : files,
1176
+ onFileFinish: (...args) => {
1177
+ console.log('args', args)
1178
+ const error = args[0]
1179
+ const fileInfo = (args as any)[2]
1180
+ if (error) {
1181
+ tempFailedFiles.push(fileInfo.Key)
1182
+ }
1183
+ onFileFinish?.apply(null, args)
1184
+ }
1185
+ })
1186
+ if (!tempFailedFiles?.length || times <= 0) return res
1187
+ if (times > 0) {
1188
+ setTimeout(
1189
+ () =>
1190
+ this.uploadFilesWithRetry({
1191
+ uploadFiles,
1192
+ options,
1193
+ times: times - 1,
1194
+ interval,
1195
+ failedFiles: tempFailedFiles
1196
+ }),
1197
+ interval
1198
+ )
1199
+ }
1200
+ }
1130
1201
  }
@@ -17,6 +17,9 @@ export interface IOptions {
17
17
  export interface IFileOptions extends IOptions {
18
18
  localPath: string;
19
19
  cloudPath?: string;
20
+ parallel?: number;
21
+ retryCount?: number;
22
+ retryInterval?: number;
20
23
  }
21
24
  export interface IFilesOptions extends IOptions {
22
25
  ignore?: string | string[];
@@ -87,6 +90,9 @@ export declare class StorageService {
87
90
  * 上传文件夹
88
91
  * @param {string} localPath 本地文件夹路径
89
92
  * @param {string} cloudPath 云端文件夹
93
+ * @param {number} parallel 并发量
94
+ * @param {number} retryCount 重试次数
95
+ * @param {number} retryInterval 重试时间间隔(毫秒)
90
96
  * @param {(string | string[])} ignore
91
97
  * @param {(string | string[])} ignore
92
98
  * @returns {Promise<void>}
@@ -96,6 +102,9 @@ export declare class StorageService {
96
102
  * 上传文件夹,支持自定义 Region 和 Bucket
97
103
  * @param {string} localPath
98
104
  * @param {string} cloudPath
105
+ * @param {number} parallel
106
+ * @param {number} retryCount
107
+ * @param {number} retryInterval
99
108
  * @param {string} bucket
100
109
  * @param {string} region
101
110
  * @param {IOptions} options
@@ -284,5 +293,15 @@ export declare class StorageService {
284
293
  * 获取存储桶配置
285
294
  */
286
295
  private getStorageConfig;
296
+ /**
297
+ * 带重试功能的上传多文件函数
298
+ * @param uploadFiles sdk上传函数
299
+ * @param options sdk上传函数参数
300
+ * @param times 重试次数
301
+ * @param interval 重试时间间隔(毫秒)
302
+ * @param failedFiles 失败文件列表
303
+ * @returns
304
+ */
305
+ private uploadFilesWithRetry;
287
306
  }
288
307
  export {};