Skip to content

Commit

Permalink
feat: signatureUrl support trafficLimit (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyie committed Mar 25, 2020
1 parent 17c2984 commit a57315f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -1817,6 +1817,7 @@ parameters:
- [Content-Type] {String} set the request content type
- [process] {String} image process params, will send with `x-oss-process`
e.g.: `{process: 'image/resize,w_200'}`
- [trafficLimit] {Number} traffic limit, range: `819200`~`838860800`.
- [response] {Object} set the response headers for download
- [content-type] {String} set the response content type
- [content-disposition] {String} set the response content disposition
Expand Down
7 changes: 6 additions & 1 deletion lib/common/signUtils.js
Expand Up @@ -112,6 +112,11 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r
subResource[processKeyword] = options.process;
}

if (options.trafficLimit) {
const trafficLimitKey = 'x-oss-traffic-limit';
subResource[trafficLimitKey] = options.trafficLimit;
}

if (options.response) {
Object.keys(options.response).forEach((k) => {
const key = `response-${k.toLowerCase()}`;
Expand All @@ -128,7 +133,7 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r
headers[key] = value;
} else if (lowerKey.indexOf('content-type') === 0) {
headers[key] = value;
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method') {
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method' && lowerKey !== 'trafficLimit') {
subResource[lowerKey] = value;
}
});
Expand Down
35 changes: 35 additions & 0 deletions test/node/object.test.js
Expand Up @@ -1038,6 +1038,41 @@ describe('test/object.test.js', () => {
// http://www.aliyun.com/darwin-v4.4.2/ali-sdk/oss/get-meta.js?OSSAccessKeyId=
assert.equal(url.indexOf('http://www.aliyun.com/'), 0);
});

it('should signature url with traffic limit', async () => {
const name = `${prefix}ali-sdk/oss/trafficLimit.js`;
let url, result;
const file_1mb = path.join(__dirname, '.tmp', 'bigfile-1mb.bin');
fs.writeFileSync(file_1mb, Buffer.alloc(1 * 1024 * 1024).fill('a\n'));

try {
url = store.signatureUrl(name, {
trafficLimit: 8 * 1024 * 100 * 4,
method: 'PUT'
})

result = await store.urllib.request(url, {
method: 'PUT',
stream: fs.createReadStream(file_1mb),
timeout: 600000,
});
assert.strictEqual(200, result.status)
} catch (error) {
assert(false, error.message)
}

try {
url = store.signatureUrl(name, {
trafficLimit: 8 * 1024 * 100 * 4,
})
result = await store.urllib.request(url, {
timeout: 600000,
});
assert.strictEqual(200, result.status)
} catch (error) {
assert(false, error.message)
}
});
});

describe('getStream()', () => {
Expand Down

0 comments on commit a57315f

Please sign in to comment.