cos.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. AppId: '', // AppId 已废弃,请拼接到 Bucket 后传入,例如:test-1250000000
  10. SecretId: '',
  11. SecretKey: '',
  12. SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
  13. ChunkRetryTimes: 2,
  14. FileParallelLimit: 3,
  15. ChunkParallelLimit: 3,
  16. ChunkSize: 1024 * 1024,
  17. SliceSize: 1024 * 1024,
  18. CopyChunkParallelLimit: 20,
  19. CopyChunkSize: 1024 * 1024 * 10,
  20. CopySliceSize: 1024 * 1024 * 10,
  21. MaxPartNumber: 10000,
  22. ProgressInterval: 1000,
  23. Domain: '',
  24. ServiceDomain: '',
  25. Protocol: '',
  26. CompatibilityMode: false,
  27. ForcePathStyle: false,
  28. UseRawKey: false,
  29. Timeout: 0, // 单位毫秒,0 代表不设置超时时间
  30. CorrectClockSkew: true,
  31. SystemClockOffset: 0, // 单位毫秒,ms
  32. UploadCheckContentMd5: false,
  33. UploadQueueSize: 1000,
  34. UploadIdCacheLimit: 500,
  35. Proxy: '',
  36. Tunnel: undefined,
  37. Ip: '',
  38. StrictSsl: true,
  39. KeepAlive: true,
  40. FollowRedirect: false,
  41. UseAccelerate: false,
  42. UserAgent: '',
  43. ConfCwd: '',
  44. ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
  45. AutoSwitchHost: true,
  46. CopySourceParser: null, // 自定义拷贝源解析器
  47. ObjectKeySimplifyCheck: true, // 开启合并校验 getObject Key
  48. // 动态秘钥,优先级Credentials > SecretId/SecretKey。注意Cred内是小写的secretId、secretKey
  49. Credentials: {
  50. secretId: '',
  51. secretKey: '',
  52. },
  53. };
  54. const watch = (obj, name, callback) => {
  55. let value = obj[name];
  56. Object.defineProperty(obj, name, {
  57. get() {
  58. return value;
  59. },
  60. set(newValue) {
  61. value = newValue;
  62. callback();
  63. },
  64. });
  65. };
  66. // 对外暴露的类
  67. var COS = function (options) {
  68. this.options = util.extend(util.clone(defaultOptions), options || {});
  69. this.options.FileParallelLimit = Math.max(1, this.options.FileParallelLimit);
  70. this.options.ChunkParallelLimit = Math.max(1, this.options.ChunkParallelLimit);
  71. this.options.ChunkRetryTimes = Math.max(0, this.options.ChunkRetryTimes);
  72. this.options.ChunkSize = Math.max(1024 * 1024, this.options.ChunkSize);
  73. this.options.CopyChunkParallelLimit = Math.max(1, this.options.CopyChunkParallelLimit);
  74. this.options.CopyChunkSize = Math.max(1024 * 1024, this.options.CopyChunkSize);
  75. this.options.CopySliceSize = Math.max(0, this.options.CopySliceSize);
  76. this.options.MaxPartNumber = Math.max(1024, Math.min(10000, this.options.MaxPartNumber));
  77. this.options.Timeout = Math.max(0, this.options.Timeout);
  78. if (this.options.AppId) {
  79. console.warn(
  80. 'warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g: "test-1250000000").'
  81. );
  82. }
  83. // 云API SDK 用小写密钥,这里兼容并 warning
  84. if (this.options.secretId || this.options.secretKey) {
  85. if (this.options.secretId && !this.options.SecretId) this.options.SecretId = this.options.secretId;
  86. if (this.options.secretKey && !this.options.SecretKey) this.options.SecretKey = this.options.secretKey;
  87. console.warn('warning: Please change options secretId/secretKey to SecretId/SecretKey.');
  88. }
  89. // 支持外部传入Cred动态秘钥
  90. if (this.options.Credentials.secretId && this.options.Credentials.secretKey) {
  91. this.options.SecretId = this.options.Credentials.secretId || '';
  92. this.options.SecretKey = this.options.Credentials.secretKey || '';
  93. }
  94. if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
  95. console.error('error: SecretId格式错误,请检查');
  96. console.error('error: SecretId format is incorrect. Please check');
  97. }
  98. if (this.options.SecretKey && this.options.SecretKey.indexOf(' ') > -1) {
  99. console.error('error: SecretKey格式错误,请检查');
  100. console.error('error: SecretKey format is incorrect. Please check');
  101. }
  102. if (util.isWeb()) {
  103. console.log('Tip: 使用 electron 等跨平台技术可正常使用Nodejs SDK,请忽略下方浏览器环境警告');
  104. console.warn(
  105. 'warning: cos-nodejs-sdk-v5 不支持浏览器使用,请改用 cos-js-sdk-v5,参考文档: https://cloud.tencent.com/document/product/436/11459'
  106. );
  107. console.warn(
  108. 'warning: cos-nodejs-sdk-v5 does not support browsers. Please use cos-js-sdk-v5 instead, See: https://cloud.tencent.com/document/product/436/11459'
  109. );
  110. }
  111. if (this.options.ForcePathStyle) {
  112. console.warn(
  113. 'cos-nodejs-sdk-v5不再支持使用path-style,仅支持使用virtual-hosted-style,参考文档:https://cloud.tencent.com/document/product/436/96243'
  114. );
  115. throw new Error('ForcePathStyle is not supported');
  116. }
  117. event.init(this);
  118. task.init(this);
  119. // 支持动态秘钥,监听到cred里secretId、secretKey变化时,主动给cos替换秘钥
  120. watch(this.options.Credentials, 'secretId', () => {
  121. console.log('Credentials secretId changed');
  122. this.options.SecretId = this.options.Credentials.secretId;
  123. });
  124. watch(this.options.Credentials, 'secretKey', () => {
  125. console.log('Credentials secretKey changed');
  126. this.options.SecretKey = this.options.Credentials.secretKey;
  127. });
  128. };
  129. base.init(COS, task);
  130. advance.init(COS, task);
  131. COS.util = {
  132. md5: util.md5,
  133. xml2json: util.xml2json,
  134. json2xml: util.json2xml,
  135. encodeBase64: util.encodeBase64,
  136. };
  137. COS.getAuthorization = util.getAuth;
  138. COS.version = pkg.version;
  139. module.exports = COS;