diff --git a/.eslintignore b/.eslintignore index 28baaae9..677069b6 100644 --- a/.eslintignore +++ b/.eslintignore @@ -21,5 +21,7 @@ dist/ lib/common/utils/createRequest.js lib/common/utils/encodeString.js lib/common/utils/getStandardRegion.js +lib/common/utils/checkEnv.js +lib/common/utils/isDingTalk.js lib/common/bucket/putBucketInventory.d.ts \ No newline at end of file diff --git a/.prettierignore b/.prettierignore index 23dd2e9b..297b08d5 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,5 +4,7 @@ dist/ lib/common/utils/createRequest.js lib/common/utils/encodeString.js lib/common/utils/getStandardRegion.js +lib/common/utils/checkEnv.js +lib/common/utils/isDingTalk.js lib/common/bucket/putBucketInventory.d.ts \ No newline at end of file diff --git a/README.md b/README.md index ce035339..39420d69 100644 --- a/README.md +++ b/README.md @@ -2078,8 +2078,9 @@ Get an object from the bucket. parameters: - name {String} object name store on OSS -- [file] {String|WriteStream} file path or WriteStream instance to store the content +- [file] {String|WriteStream|Object} file path or WriteStream instance to store the content If `file` is null or ignore this parameter, function will return info contains `content` property. + If `file` is Object, it will be treated as options. - [options] {Object} optional parameters - [versionId] {String} the version id of history object - [timeout] {Number} the operation timeout @@ -2157,6 +2158,13 @@ await store.get('ossdemo/not-exists-demo.txt', filepath, { }); ``` +- If `file` is Object, it will be treated as options. + +```js +const versionId = 'versionId string'; +await store.get('ossdemo/not-exists-demo.txt', { versionId }); +``` + ### .getStream(name[, options]) Get an object read stream. diff --git a/lib/common/object/get.js b/lib/common/object/get.js index 2ce5e194..f119b6a4 100644 --- a/lib/common/object/get.js +++ b/lib/common/object/get.js @@ -1,11 +1,12 @@ const fs = require('fs'); const is = require('is-type-of'); +const { isObject } = require('../utils/isObject'); const proto = exports; /** * get * @param {String} name - object name - * @param {String | Stream} file + * @param {String | Stream | Object} file - file path or file stream or options * @param {Object} options * @param {{res}} */ @@ -18,7 +19,7 @@ proto.get = async function get(name, file, options = {}) { } else if (is.string(file)) { writeStream = fs.createWriteStream(file); needDestroy = true; - } else { + } else if (isObject(file)) { // get(name, options) options = file; } diff --git a/lib/common/object/head.js b/lib/common/object/head.js index 8cbcd30d..1d51e268 100644 --- a/lib/common/object/head.js +++ b/lib/common/object/head.js @@ -1,4 +1,5 @@ const { checkEnv } = require('../utils/checkEnv'); + const proto = exports; /** * head diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 6658198e..43498415 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -694,6 +694,41 @@ describe('browser', () => { }); assert(!requestUrls2[0].includes('response-cache-control=no-cache')); }); + + it('test file is not a stream or string', async () => { + let result = await store.get(name, null, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, 2, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, undefined, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + result = await store.get(name, true, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + }); + it('test file is options', async () => { + const result = await store.get(name, { + headers: { + Range: 'bytes=0-3' + } + }); + assert.equal(result.res.headers['content-length'], '4'); + }); }); describe('put', () => { diff --git a/test/node/object.test.js b/test/node/object.test.js index 48a5883b..992b7bd0 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -884,6 +884,44 @@ describe('test/object.test.js', () => { ); }); + it('test file is not a stream or string', async () => { + let result = await store.get(name, null, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + assert(Buffer.isBuffer(result.content), 'content should be Buffer'); + result = await store.get(name, undefined, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + result = await store.get(name, 1, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + result = await store.get(name, true, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + }); + + it('test file is options', async () => { + const result = await store.get(name, { + headers: { + Range: 'bytes=0-9' + } + }); + assert.equal(result.res.headers['content-length'], '10'); + assert(Buffer.isBuffer(result.content), 'content should be Buffer'); + }); + describe('If-Modified-Since header', () => { it('should 200 when If-Modified-Since < object modified time', async () => { let lastYear = new Date(resHeaders.date);