UNPKG

ks3

Version:

本代码库为`金山云存储KS3`服务.主要提供`KS3 nodejs SDK`和`命令行工具`.

118 lines (101 loc) 3.43 kB
const ms = require('humanize-ms'); const urlUtil = require('url') const config = require('../config') function setEndpoint ({ endpoint, internal }) { let ep = '' const url = urlUtil.parse(endpoint) const isFullUrl = endpoint.startsWith('http') if (isFullUrl) { ep = `${url.host}` } else { ep = `${endpoint}` } let suffix = '.ksyuncs.com' if (internal) { ep = ep.replace(suffix, '-internal' + suffix) } return ep } /** * 检查endpoint是否符合ksyun标准域名https://docs.ksyun.com/documents/6761 * @param {*} endpoint * @returns */ function checkKs3Endpoint (endpoint) { return endpoint.indexOf('ksyuncs.com') > 0 } /** * 将字符串首字母转小写,比如 Bucket --> bucket * @param {*} str * @returns */ function lowerFirst (str) { if (str == 'AK') return 'ak' if (str == 'SK') return 'sk' return str.charAt(0).toLowerCase() + str.slice(1) } /** * 将接收到的不同的参数转成统一的形式,比如 { AK, SK } / { ak, sk} --> {ak, sk} * * @param {*} opt * @returns */ function convertOptions (opt) { if (!opt) return '' var obj = {} for(var key in opt) { const tmpKey = lowerFirst(key) obj[tmpKey] = opt[key] } return obj } module.exports = function (options) { // 为了兼容性 如果options属性均为大驼峰 则转成首字母小写 options = convertOptions(options) // 处理region if (options.region && !options.endpoint) { options.endpoint = config.ENDPOINT[options.region] } const opt = Object.assign({ ak: '', sk: '', bucket: '', region: 'BEIJING',// 只参与计算,目前可不填 endpoint: '', // 不需要http://,只填写host即可 timeout: 3 * 60 * 1000, // 超时时间,默认值为3min,单位为毫秒。 internal: false, // 是否使用内网 domainMode: false, // 是否支持自定义域名,默认值为falsesecure: false, // 设置secure为true,则使用HTTPS;设置secure为false,则使用HTTP securityToken: '', // 临时角色token followRedirect: true, // 如禁用自动重定向,需设置为false proxyUrl: '', // 代理 URL,示例:http://example.com:3128 proxyAuth: { user: '', // 代理认证的用户名 pass: '' // 代理认证的密码 }, maxRetryTimes: 3, // 出错重试次数,默认3次(总共请求4次) retryStrategy: 'ExponentialWait', // 重试策略,支持设置为:ExponentialWait、FixedWait、LinearWait、JitterWait,默认值为ExponentialWait dnsCache: true, // 是否开启 DNS 缓存 dnsCacheTimeout: 30 * 1000, // DNS缓存清理时间,默认30s }, options) opt.ak = opt.ak.trim() opt.sk = opt.sk.trim() if (opt.timeout) { opt.timeout = ms(opt.timeout) } if (!opt.domainMode) { if (!checkKs3Endpoint(options.endpoint)) { throw new Error('if domainMode is false, endpoint value should be ks3 host!') } const param = { endpoint: opt.endpoint, internal: opt.internal } opt.endpoint = setEndpoint(param) } opt.inited = true // bucket更替为bucketName if (opt.bucket) opt.bucketName = opt.bucket delete opt.bucket return opt }