cos.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. 'use strict';
  2. var util = require('./util');
  3. var event = require('./event');
  4. var task = require('./task');
  5. var base = require('./base');
  6. var advance = require('./advance');
  7. var defaultOptions = {
  8. SecretId: '',
  9. SecretKey: '',
  10. SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
  11. ChunkRetryTimes: 2,
  12. FileParallelLimit: 3,
  13. ChunkParallelLimit: 3,
  14. ChunkSize: 1024 * 1024,
  15. SliceSize: 1024 * 1024,
  16. CopyChunkParallelLimit: 20,
  17. CopyChunkSize: 1024 * 1024 * 10,
  18. CopySliceSize: 1024 * 1024 * 10,
  19. MaxPartNumber: 10000,
  20. ProgressInterval: 1000,
  21. UploadQueueSize: 10000,
  22. Domain: '',
  23. ServiceDomain: '',
  24. Protocol: '',
  25. CompatibilityMode: false,
  26. ForcePathStyle: false,
  27. Timeout: 0, // 单位毫秒,0 代表不设置超时时间
  28. CorrectClockSkew: true,
  29. SystemClockOffset: 0, // 单位毫秒,ms
  30. UploadCheckContentMd5: false,
  31. UploadIdCacheLimit: 50,
  32. UseAccelerate: false,
  33. ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
  34. HttpDNSServiceId: '', // HttpDNS 服务商 Id,填写后代表开启 HttpDNS 服务。HttpDNS 用法详见https://developers.weixin.qq.com/miniprogram/dev/framework/ability/HTTPDNS.html
  35. };
  36. // 对外暴露的类
  37. var COS = function (options) {
  38. this.options = util.extend(util.clone(defaultOptions), options || {});
  39. this.options.FileParallelLimit = Math.max(1, this.options.FileParallelLimit);
  40. this.options.ChunkParallelLimit = Math.max(1, this.options.ChunkParallelLimit);
  41. this.options.ChunkRetryTimes = Math.max(0, this.options.ChunkRetryTimes);
  42. this.options.ChunkSize = Math.max(1024 * 1024, this.options.ChunkSize);
  43. this.options.CopyChunkParallelLimit = Math.max(1, this.options.CopyChunkParallelLimit);
  44. this.options.CopyChunkSize = Math.max(1024 * 1024, this.options.CopyChunkSize);
  45. this.options.CopySliceSize = Math.max(0, this.options.CopySliceSize);
  46. this.options.MaxPartNumber = Math.max(1024, Math.min(10000, this.options.MaxPartNumber));
  47. this.options.Timeout = Math.max(0, this.options.Timeout);
  48. if (this.options.AppId) {
  49. console.warn('warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g: "test-1250000000").');
  50. }
  51. if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
  52. console.error('error: SecretId格式错误,请检查');
  53. console.error('error: SecretId format is incorrect. Please check');
  54. }
  55. if (this.options.SecretKey && this.options.SecretKey.indexOf(' ') > -1) {
  56. console.error('error: SecretKey格式错误,请检查');
  57. console.error('error: SecretKey format is incorrect. Please check');
  58. }
  59. event.init(this);
  60. task.init(this);
  61. };
  62. base.init(COS, task);
  63. advance.init(COS, task);
  64. COS.util = {
  65. md5: util.md5,
  66. xml2json: util.xml2json,
  67. json2xml: util.json2xml,
  68. };
  69. COS.getAuthorization = util.getAuth;
  70. COS.version = '1.2.1';
  71. module.exports = COS;