cos.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. AppId: '', // AppId 已废弃,请拼接到 Bucket 后传入,例如:test-1250000000
  9. SecretId: '',
  10. SecretKey: '',
  11. SecurityToken: '', // 使用临时密钥需要注意自行刷新 Token
  12. ChunkRetryTimes: 2,
  13. FileParallelLimit: 3,
  14. ChunkParallelLimit: 3,
  15. ChunkSize: 1024 * 1024,
  16. SliceSize: 1024 * 1024,
  17. CopyChunkParallelLimit: 20,
  18. CopyChunkSize: 1024 * 1024 * 10,
  19. CopySliceSize: 1024 * 1024 * 10,
  20. MaxPartNumber: 10000,
  21. ProgressInterval: 1000,
  22. Domain: '',
  23. ServiceDomain: '',
  24. Protocol: '',
  25. CompatibilityMode: false,
  26. ForcePathStyle: false,
  27. UseRawKey: false,
  28. Timeout: 0, // 单位毫秒,0 代表不设置超时时间
  29. CorrectClockSkew: true,
  30. SystemClockOffset: 0, // 单位毫秒,ms
  31. UploadCheckContentMd5: false,
  32. UploadQueueSize: 10000,
  33. UploadAddMetaMd5: false,
  34. UploadIdCacheLimit: 50,
  35. UseAccelerate: false,
  36. ForceSignHost: true, // 默认将host加入签名计算,关闭后可能导致越权风险,建议保持为true
  37. };
  38. // 对外暴露的类
  39. var COS = function (options) {
  40. this.options = util.extend(util.clone(defaultOptions), options || {});
  41. this.options.FileParallelLimit = Math.max(1, this.options.FileParallelLimit);
  42. this.options.ChunkParallelLimit = Math.max(1, this.options.ChunkParallelLimit);
  43. this.options.ChunkRetryTimes = Math.max(0, this.options.ChunkRetryTimes);
  44. this.options.ChunkSize = Math.max(1024 * 1024, this.options.ChunkSize);
  45. this.options.CopyChunkParallelLimit = Math.max(1, this.options.CopyChunkParallelLimit);
  46. this.options.CopyChunkSize = Math.max(1024 * 1024, this.options.CopyChunkSize);
  47. this.options.CopySliceSize = Math.max(0, this.options.CopySliceSize);
  48. this.options.MaxPartNumber = Math.max(1024, Math.min(10000, this.options.MaxPartNumber));
  49. this.options.Timeout = Math.max(0, this.options.Timeout);
  50. if (this.options.AppId) {
  51. console.warn('warning: AppId has been deprecated, Please put it at the end of parameter Bucket(E.g: "test-1250000000").');
  52. }
  53. if (this.options.SecretId && this.options.SecretId.indexOf(' ') > -1) {
  54. console.error('error: SecretId格式错误,请检查');
  55. console.error('error: SecretId format is incorrect. Please check');
  56. }
  57. if (this.options.SecretKey && this.options.SecretKey.indexOf(' ') > -1) {
  58. console.error('error: SecretKey格式错误,请检查');
  59. console.error('error: SecretKey format is incorrect. Please check');
  60. }
  61. if (util.isNode()) {
  62. console.warn('warning: cos-js-sdk-v5 不支持 nodejs 环境使用,请改用 cos-nodejs-sdk-v5,参考文档: https://cloud.tencent.com/document/product/436/8629');
  63. console.warn('warning: cos-js-sdk-v5 does not support nodejs environment. Please use cos-nodejs-sdk-v5 instead. See: https://cloud.tencent.com/document/product/436/8629');
  64. }
  65. event.init(this);
  66. task.init(this);
  67. };
  68. base.init(COS, task);
  69. advance.init(COS, task);
  70. COS.util = {
  71. md5: util.md5,
  72. xml2json: util.xml2json,
  73. json2xml: util.json2xml,
  74. };
  75. COS.getAuthorization = util.getAuth;
  76. COS.version = '1.3.9';
  77. module.exports = COS;