diff --git a/README.md b/README.md index 203860350..c6e09a521 100644 --- a/README.md +++ b/README.md @@ -1666,7 +1666,7 @@ parameters: - name {String} object name store on OSS - file {String|Buffer|ReadStream|File(only support Browser)|Blob(only support Browser)} object local path, content buffer or ReadStream content instance use in Node, Blob and html5 File - [options] {Object} optional parameters - - [timeout] {Number} the operation timeout + - [timeout] {Number} the operation timeout (ms) - [mime] {String} custom mime, will send with `Content-Type` entity header - [meta] {Object} user meta, will send with `x-oss-meta-` prefix string e.g.: `{ uid: 123, pid: 110 }` @@ -4557,7 +4557,29 @@ Each error return by OSS server will contains these properties: you can send this request id to OSS engineer to find out what's happend. - hostId {String} OSS cluster name for this request -The following table lists the OSS error codes: +### ResponseTimeoutError + +The default timeout is 60 seconds. Please set the timeout as needed. The timeout unit is milliseconds. + +```javascript +client.get('example.txt', { timeout: 60000 * 2 }); + +client.get('example.txt', { headers: { Range: `bytes=0-${1024 * 1024 * 100}` } }); // Download the first 100MB +``` + +### ConnectionTimeoutError + +The network link timed out. Please check the network status. If there is no problem with the network, please reduce the partSize or increase the timeout. + +```javascript +const client = new OSS({ ak, sk, retryMax: 10 }); + +client.multipartUpload('example.txt', { timeout: 60000 * 2 }); + +client.multipartUpload('example.txt', { partSize: 1024 * 512 }); // partSize 512KB +``` + +### The following table lists the OSS error codes: [More code info](https://help.aliyun.com/knowledge_detail/32005.html) diff --git a/lib/client.js b/lib/client.js index 520323714..421b500fe 100644 --- a/lib/client.js +++ b/lib/client.js @@ -223,7 +223,14 @@ async function request(params) { } if (err.name === 'ResponseTimeoutError') { - err.message = `${err.message.split(',')[0]}, please increase the timeout or use multipartDownload.`; + err.message = `${ + err.message.split(',')[0] + }, please increase the timeout, see more details at https://github.com/ali-sdk/ali-oss#responsetimeouterror`; + } + if (err.name === 'ConnectionTimeoutError') { + err.message = `${ + err.message.split(',')[0] + }, please increase the timeout or reduce the partSize, see more details at https://github.com/ali-sdk/ali-oss#connectiontimeouterror`; } throw err; } diff --git a/lib/common/object/head.js b/lib/common/object/head.js index 11964294a..8cbcd30d3 100644 --- a/lib/common/object/head.js +++ b/lib/common/object/head.js @@ -1,3 +1,4 @@ +const { checkEnv } = require('../utils/checkEnv'); const proto = exports; /** * head @@ -7,6 +8,9 @@ const proto = exports; */ proto.head = async function head(name, options = {}) { + checkEnv( + 'Because HeadObject has gzip enabled, head cannot get the file size correctly. If you need to get the file size, please use getObjectMeta' + ); options.subres = Object.assign({}, options.subres); if (options.versionId) { options.subres.versionId = options.versionId; diff --git a/lib/common/utils/checkEnv.d.ts b/lib/common/utils/checkEnv.d.ts new file mode 100644 index 000000000..e536606ae --- /dev/null +++ b/lib/common/utils/checkEnv.d.ts @@ -0,0 +1 @@ +export declare function checkEnv(msg: string): void; diff --git a/lib/common/utils/checkEnv.js b/lib/common/utils/checkEnv.js new file mode 100644 index 000000000..1a0f7b2c8 --- /dev/null +++ b/lib/common/utils/checkEnv.js @@ -0,0 +1,9 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.checkEnv = void 0; +function checkEnv(msg) { + if (process.browser) { + console.warn(msg); + } +} +exports.checkEnv = checkEnv; diff --git a/lib/common/utils/checkEnv.ts b/lib/common/utils/checkEnv.ts new file mode 100644 index 000000000..b37365808 --- /dev/null +++ b/lib/common/utils/checkEnv.ts @@ -0,0 +1,5 @@ +export function checkEnv(msg: string) { + if (process.browser) { + console.warn(msg); + } +} diff --git a/lib/common/utils/createRequest.js b/lib/common/utils/createRequest.js index 1842f3934..3dbec395f 100644 --- a/lib/common/utils/createRequest.js +++ b/lib/common/utils/createRequest.js @@ -11,6 +11,7 @@ const { encoder } = require('./encoder'); const { isIP } = require('./isIP'); const { setRegion } = require('./setRegion'); const { getReqUrl } = require('../client/getReqUrl'); +const { isDingTalk } = require('./isDingTalk'); function getHeader(headers, name) { return headers[name] || headers[name.toLowerCase()]; } @@ -43,6 +44,9 @@ function createRequest(params) { if (params.mime && params.mime.indexOf('/') > 0) { headers['Content-Type'] = params.mime; } + else if (isDingTalk()) { + headers['Content-Type'] = 'application/octet-stream'; + } else { headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || '')); } diff --git a/lib/common/utils/createRequest.ts b/lib/common/utils/createRequest.ts index ad03d7938..dfbbf0618 100644 --- a/lib/common/utils/createRequest.ts +++ b/lib/common/utils/createRequest.ts @@ -8,6 +8,7 @@ const { encoder } = require('./encoder'); const { isIP } = require('./isIP'); const { setRegion } = require('./setRegion'); const { getReqUrl } = require('../client/getReqUrl'); +const { isDingTalk } = require('./isDingTalk'); interface Headers { [propName: string]: any; @@ -58,6 +59,8 @@ export function createRequest(this: any, params) { if (!getHeader(headers, 'Content-Type')) { if (params.mime && params.mime.indexOf('/') > 0) { headers['Content-Type'] = params.mime; + } else if (isDingTalk()) { + headers['Content-Type'] = 'application/octet-stream'; } else { headers['Content-Type'] = mime.getType(params.mime || path.extname(params.object || '')); } diff --git a/lib/common/utils/isDingTalk.d.ts b/lib/common/utils/isDingTalk.d.ts new file mode 100644 index 000000000..42cb892b3 --- /dev/null +++ b/lib/common/utils/isDingTalk.d.ts @@ -0,0 +1 @@ +export declare function isDingTalk(): boolean; diff --git a/lib/common/utils/isDingTalk.js b/lib/common/utils/isDingTalk.js new file mode 100644 index 000000000..04d89d9e4 --- /dev/null +++ b/lib/common/utils/isDingTalk.js @@ -0,0 +1,10 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.isDingTalk = void 0; +function isDingTalk() { + if (process.browser && window.navigator.userAgent.toLowerCase().includes('aliapp(dingtalk')) { + return true; + } + return false; +} +exports.isDingTalk = isDingTalk; diff --git a/lib/common/utils/isDingTalk.ts b/lib/common/utils/isDingTalk.ts new file mode 100644 index 000000000..aa7ff33b4 --- /dev/null +++ b/lib/common/utils/isDingTalk.ts @@ -0,0 +1,6 @@ +export function isDingTalk() { + if (process.browser && window.navigator.userAgent.toLowerCase().includes('aliapp(dingtalk')) { + return true; + } + return false; +} diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 7f3fb930b..75c4ea897 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -726,9 +726,6 @@ describe('browser', () => { timeout: 300 }; try { - setTimeout(() => { - options.timeout = 60000; - }, 200); await store.put(name, body, options); assert(false); } catch (error) {