租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.1 KiB

3 months ago
  1. const ms = require('humanize-ms');
  2. const urlutil = require('url');
  3. const { checkBucketName: _checkBucketName } = require('../utils/checkBucketName');
  4. const { setRegion } = require('../utils/setRegion');
  5. const { checkConfigValid } = require('../utils/checkConfigValid');
  6. function setEndpoint(endpoint, secure) {
  7. checkConfigValid(endpoint, 'endpoint');
  8. let url = urlutil.parse(endpoint);
  9. if (!url.protocol) {
  10. url = urlutil.parse(`http${secure ? 's' : ''}://${endpoint}`);
  11. }
  12. if (url.protocol !== 'http:' && url.protocol !== 'https:') {
  13. throw new Error('Endpoint protocol must be http or https.');
  14. }
  15. return url;
  16. }
  17. module.exports = function (options) {
  18. if (!options || !options.accessKeyId || !options.accessKeySecret) {
  19. throw new Error('require accessKeyId, accessKeySecret');
  20. }
  21. if (options.stsToken && !options.refreshSTSToken && !options.refreshSTSTokenInterval) {
  22. console.warn(
  23. "It's recommended to set 'refreshSTSToken' and 'refreshSTSTokenInterval' to refresh" +
  24. ' stsToken、accessKeyId、accessKeySecret automatically when sts token has expired'
  25. );
  26. }
  27. if (options.bucket) {
  28. _checkBucketName(options.bucket);
  29. }
  30. const opts = Object.assign(
  31. {
  32. region: 'oss-cn-hangzhou',
  33. internal: false,
  34. secure: false,
  35. timeout: 60000,
  36. bucket: null,
  37. endpoint: null,
  38. cname: false,
  39. isRequestPay: false,
  40. sldEnable: false,
  41. headerEncoding: 'utf-8',
  42. refreshSTSToken: null,
  43. refreshSTSTokenInterval: 60000 * 5,
  44. retryMax: 0,
  45. authorizationV4: false // 启用v4签名,默认关闭
  46. },
  47. options
  48. );
  49. opts.accessKeyId = opts.accessKeyId.trim();
  50. opts.accessKeySecret = opts.accessKeySecret.trim();
  51. if (opts.timeout) {
  52. opts.timeout = ms(opts.timeout);
  53. }
  54. if (opts.endpoint) {
  55. opts.endpoint = setEndpoint(opts.endpoint, opts.secure);
  56. } else if (opts.region) {
  57. opts.endpoint = setRegion(opts.region, opts.internal, opts.secure);
  58. } else {
  59. throw new Error('require options.endpoint or options.region');
  60. }
  61. opts.inited = true;
  62. return opts;
  63. };