cos.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 pkg = require('../package.json');
  8. var defaultOptions = {
  9. SecretId: '',
  10. SecretKey: '',
  11. SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
  12. StartTime: 0, // 临时密钥返回起始时间
  13. ExpiredTime: 0, // 临时密钥过期时间
  14. ChunkRetryTimes: 2,
  15. FileParallelLimit: 3,
  16. ChunkParallelLimit: 3,
  17. ChunkSize: 1024 * 1024,
  18. SliceSize: 1024 * 1024,
  19. CopyChunkParallelLimit: 20,
  20. CopyChunkSize: 1024 * 1024 * 10,
  21. CopySliceSize: 1024 * 1024 * 10,
  22. MaxPartNumber: 10000,
  23. ProgressInterval: 1000,
  24. UploadQueueSize: 10000,
  25. Domain: '',
  26. ServiceDomain: '',
  27. Protocol: '',
  28. CompatibilityMode: false,
  29. ForcePathStyle: false,
  30. Timeout: 0, // 单位毫秒,0 代表不设置超时时间
  31. CorrectClockSkew: true,
  32. SystemClockOffset: 0, // 单位毫秒,ms
  33. UploadCheckContentMd5: false,
  34. UploadAddMetaMd5: false,
  35. UploadIdCacheLimit: 50,
  36. UseAccelerate: false,
  37. ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
  38. HttpDNSServiceId: '', // HttpDNS 服务商 Id,填写后代表开启 HttpDNS 服务。HttpDNS 用法详见https://developers.weixin.qq.com/miniprogram/dev/framework/ability/HTTPDNS.html
  39. SimpleUploadMethod: 'postObject', // 高级上传内部判断需要走简单上传时,指定的上传方法,可选postObject或putObject
  40. AutoSwitchHost: false,
  41. CopySourceParser: null, // 自定义拷贝源解析器
  42. ObjectKeySimplifyCheck: true, // 开启合并校验 getObject Key
  43. /** 上报相关配置 **/
  44. DeepTracker: false, // 上报时是否对每个分块上传做单独上报
  45. TrackerDelay: 5000, // 周期性上报,单位毫秒。0代表实时上报
  46. CustomId: '', // 自定义上报id
  47. BeaconReporter: null, // 灯塔上报组件,如有需要请自行传入,传入即代表开启上报
  48. ClsReporter: null, // cls 上报组件,如有需要请自行传入,传入即代表开启上报
  49. };
  50. // 对外暴露的类
  51. var COS = function (options) {
  52. this.options = util.extend(util.clone(defaultOptions), options || {});
  53. this.options.FileParallelLimit = Math.max(1, this.options.FileParallelLimit);
  54. this.options.ChunkParallelLimit = Math.max(1, this.options.ChunkParallelLimit);
  55. this.options.ChunkRetryTimes = Math.max(0, this.options.ChunkRetryTimes);
  56. this.options.ChunkSize = Math.max(1024 * 1024, this.options.ChunkSize);
  57. this.options.CopyChunkParallelLimit = Math.max(1, this.options.CopyChunkParallelLimit);
  58. this.options.CopyChunkSize = Math.max(1024 * 1024, this.options.CopyChunkSize);
  59. this.options.CopySliceSize = Math.max(0, this.options.CopySliceSize);
  60. this.options.MaxPartNumber = Math.max(1024, Math.min(10000, this.options.MaxPartNumber));
  61. this.options.Timeout = Math.max(0, this.options.Timeout);
  62. this.options.EnableReporter = this.options.BeaconReporter || this.options.ClsReporter;
  63. if (this.options.AppId) {
  64. console.warn(
  65. 'warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g: "test-1250000000").'
  66. );
  67. }
  68. if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
  69. console.error('error: SecretId格式错误,请检查');
  70. console.error('error: SecretId format is incorrect. Please check');
  71. }
  72. if (this.options.SecretKey && this.options.SecretKey.indexOf(' ') > -1) {
  73. console.error('error: SecretKey格式错误,请检查');
  74. console.error('error: SecretKey format is incorrect. Please check');
  75. }
  76. if (this.options.ForcePathStyle) {
  77. console.warn(
  78. 'cos-wx-sdk-v5不再支持使用path-style,仅支持使用virtual-hosted-style,参考文档:https://cloud.tencent.com/document/product/436/96243'
  79. );
  80. throw new Error('ForcePathStyle is not supported');
  81. }
  82. event.init(this);
  83. task.init(this);
  84. };
  85. base.init(COS, task);
  86. advance.init(COS, task);
  87. COS.util = {
  88. md5: util.md5,
  89. xml2json: util.xml2json,
  90. json2xml: util.json2xml,
  91. encodeBase64: util.encodeBase64,
  92. };
  93. COS.getAuthorization = util.getAuth;
  94. COS.version = pkg.version;
  95. module.exports = COS;