Skip to content

Commit 46c8dec

Browse files
binghaiwangPeterRao
authored andcommitted
feat: support callback options for put and multipartUpload (#379)
* test: support callback server * test: change test name * test: add browser callback case * feat: add case callbackServer doc * fix: multipartUpload and multipartUploadCopy callback * feat: support callback and callbackVar options to put and multipartUpload * feat: review code fix * feat: modify callback readme Close #85
1 parent e42a534 commit 46c8dec

File tree

13 files changed

+349
-100
lines changed

13 files changed

+349
-100
lines changed

README.md

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -793,29 +793,21 @@ parameters:
793793
- [mime] {String} custom mime, will send with `Content-Type` entity header
794794
- [meta] {Object} user meta, will send with `x-oss-meta-` prefix string
795795
e.g.: `{ uid: 123, pid: 110 }`
796+
- [callback] {Object} The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
797+
- url {String} After a file is uploaded successfully, the OSS sends a callback request to this URL.
798+
- [host] {String} The host header value for initiating callback requests.
799+
- body {String} The value of the request body when a callback is initiated, for example, key=$(key)&etag=$(etag)&my_var=$(x:my_var).
800+
- [contentType] {String} The Content-Type of the callback requests initiatiated, It supports application/x-www-form-urlencoded and application/json, and the former is the default value.
801+
- [customValue] {Object} Custom parameters are a map of key-values<br>
802+
e.g.:
803+
```js
804+
var customValue = {var1: 'value1', var2: 'value2'}
805+
```
796806
- [headers] {Object} extra headers, detail see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616.html)
797807
- 'Cache-Control' cache control for download, e.g.: `Cache-Control: public, no-cache`
798808
- 'Content-Disposition' object name for download, e.g.: `Content-Disposition: somename`
799809
- 'Content-Encoding' object content encoding for download, e.g.: `Content-Encoding: gzip`
800810
- 'Expires' expires time (milliseconds) for download, e.g.: `Expires: 3600000`
801-
- [x-oss-callback] The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
802-
e.g.:
803-
```json
804-
{
805-
"callbackUrl":"121.101.166.30/test.php", //Required
806-
"callbackHost":"oss-cn-hangzhou.aliyuncs.com", //Optional
807-
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}", //Required
808-
"callbackBodyType":"application/json" //Optional
809-
}
810-
```
811-
- [x-oss-callback-var] Custom parameters are a map of key-values. You can configure the required parameters to the map. When initiating a POST callback request, the OSS puts these parameters and the system parameters described in the preceding section in the body of the POST request, so that these parameters can be easily obtained by the callback recipient.detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm) Custom parameters<br>
812-
e.g.: need to use Base64 to encode
813-
```json
814-
{
815-
"x:var1":"value1",
816-
"x:var2":"value2"
817-
}
818-
```
819811

820812
Success will return the object information.
821813

@@ -921,6 +913,16 @@ parameters:
921913
- [mime] {String} custom mime, will send with `Content-Type` entity header
922914
- [meta] {Object} user meta, will send with `x-oss-meta-` prefix string
923915
e.g.: `{ uid: 123, pid: 110 }`
916+
- [callback] {Object} The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
917+
- url {String} After a file is uploaded successfully, the OSS sends a callback request to this URL.
918+
- [host] {String} The host header value for initiating callback requests.
919+
- body {String} The value of the request body when a callback is initiated, for example, key=$(key)&etag=$(etag)&my_var=$(x:my_var).
920+
- [contentType] {String} The Content-Type of the callback requests initiatiated, It supports application/x-www-form-urlencoded and application/json, and the former is the default value.
921+
- [customValue] {Object} Custom parameters are a map of key-values<br>
922+
e.g.:
923+
```js
924+
var customValue = {var1: 'value1', var2: 'value2'}
925+
```
924926
- [headers] {Object} extra headers, detail see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616.html)
925927
- 'Cache-Control' cache control for download, e.g.: `Cache-Control: public, no-cache`
926928
- 'Content-Disposition' object name for download, e.g.: `Content-Disposition: somename`
@@ -1709,25 +1711,17 @@ parameters:
17091711
- etag {String} object etag contains ", e.g.: "5B3C1A2E053D763E1B002CC607C5A0FE"
17101712
- [options] {Object} optional parameters
17111713
- [timeout] {Number} the operation timeout
1712-
- [headers] {Object} extra headers, detail see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616.html)
1713-
- [x-oss-callback] The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
1714-
e.g.:
1715-
```json
1716-
{
1717-
"callbackUrl":"121.101.166.30/test.php", //Required
1718-
"callbackHost":"oss-cn-hangzhou.aliyuncs.com", //Optional
1719-
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}", //Required
1720-
"callbackBodyType":"application/json" //Optional
1721-
}
1722-
```
1723-
- [x-oss-callback-var] Custom parameters are a map of key-values. You can configure the required parameters to the map. When initiating a POST callback request, the OSS puts these parameters and the system parameters described in the preceding section in the body of the POST request, so that these parameters can be easily obtained by the callback recipient.detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm) Custom parameters<br>
1724-
e.g.: need to use Base64 to encode
1725-
```json
1726-
{
1727-
"x:var1":"value1",
1728-
"x:var2":"value2"
1729-
}
1714+
- [callback] {Object} The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
1715+
- url {String} After a file is uploaded successfully, the OSS sends a callback request to this URL.
1716+
- [host] {String} The host header value for initiating callback requests.
1717+
- body {String} The value of the request body when a callback is initiated, for example, key=$(key)&etag=$(etag)&my_var=$(x:my_var).
1718+
- [contentType] {String} The Content-Type of the callback requests initiatiated, It supports application/x-www-form-urlencoded and application/json, and the former is the default value.
1719+
- [customValue] {Object} Custom parameters are a map of key-values<br>
1720+
e.g.:
1721+
```js
1722+
var customValue = {var1: 'value1', var2: 'value2'}
17301723
```
1724+
- [headers] {Object} extra headers, detail see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616.html)
17311725
17321726
17331727
Success will return:
@@ -1794,30 +1788,22 @@ parameters:
17941788
otherwise a new multipart upload will be created.
17951789
- [meta] {Object} user meta, will send with `x-oss-meta-` prefix string
17961790
- [mime] {String} custom mime , will send with `Content-Type` entity header
1791+
- [callback] {Object} The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
1792+
- url {String} After a file is uploaded successfully, the OSS sends a callback request to this URL.
1793+
- [host] {String} The host header value for initiating callback requests.
1794+
- body {String} The value of the request body when a callback is initiated, for example, key=$(key)&etag=$(etag)&my_var=$(x:my_var).
1795+
- [contentType] {String} The Content-Type of the callback requests initiatiated, It supports application/x-www-form-urlencoded and application/json, and the former is the default value.
1796+
- [customValue] {Object} Custom parameters are a map of key-values<br>
1797+
e.g.:
1798+
```js
1799+
var customValue = {var1: 'value1', var2: 'value2'}
1800+
```
17971801
- [headers] {Object} extra headers, detail see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616.html)
17981802
- 'Cache-Control' cache control for download, e.g.: `Cache-Control: public, no-cache`
17991803
- 'Content-Disposition' object name for download, e.g.: `Content-Disposition: somename`
18001804
- 'Content-Encoding' object content encoding for download, e.g.: `Content-Encoding: gzip`
18011805
- 'Expires' expires time (milliseconds) for download, e.g.: `Expires: 3600000`
18021806
- **NOTE**: Some headers are [disabled in browser][disabled-browser-headers]
1803-
- [x-oss-callback] The callback parameter is composed of a JSON string encoded in Base64,detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm)<br>
1804-
e.g.:
1805-
```json
1806-
{
1807-
"callbackUrl":"121.101.166.30/test.php", //Required
1808-
"callbackHost":"oss-cn-hangzhou.aliyuncs.com", //Optional
1809-
"callbackBody":"{\"mimeType\":${mimeType},\"size\":${size}}", //Required
1810-
"callbackBodyType":"application/json" //Optional
1811-
}
1812-
```
1813-
- [x-oss-callback-var] Custom parameters are a map of key-values. You can configure the required parameters to the map. When initiating a POST callback request, the OSS puts these parameters and the system parameters described in the preceding section in the body of the POST request, so that these parameters can be easily obtained by the callback recipient.detail [see](https://www.alibabacloud.com/help/doc-detail/31989.htm) Custom parameters<br>
1814-
e.g.: need to use Base64 to encode
1815-
```json
1816-
{
1817-
"x:var1":"value1",
1818-
"x:var2":"value2"
1819-
}
1820-
```
18211807
- [timeout] {Number} Milliseconds before a request is considered to be timed out
18221808
18231809
Success will return:

lib/browser/managed_upload.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ var proto = exports;
1616
* @param {String} name
1717
* @param {String|File} file
1818
* @param {Object} options
19+
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
20+
* {String} options.callback.url the OSS sends a callback request to this URL
21+
* {String} options.callback.host The host header value for initiating callback requests
22+
* {String} options.callback.body The value of the request body when a callback is initiated
23+
* {String} options.callback.contentType The Content-Type of the callback requests initiatiated
24+
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
25+
* customValue = {
26+
* key1: 'value1',
27+
* key2: 'value2'
28+
* }
1929
*/
2030
proto.multipartUpload = function* multipartUpload(name, file, options) {
2131
this.resetCancelFlag();
@@ -108,7 +118,7 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
108118
size: pi.end - pi.start
109119
};
110120

111-
var result = yield self._uploadPart(name, uploadId, partNo, data, options);
121+
var result = yield self._uploadPart(name, uploadId, partNo, data);
112122
doneParts.push({
113123
number: partNo,
114124
etag: result.res.headers.etag

lib/browser/object.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var urlutil = require('url');
1010
var copy = require('copy-to');
1111
var path = require('path');
1212
var mime = require('mime');
13+
var callback = require('../common/callback');
1314
// var assert = require('assert');
1415

1516

@@ -45,6 +46,16 @@ proto.append = function* (name, file, options) {
4546
* @param {String} name the object key
4647
* @param {Mixed} file String(file path)/Buffer/ReadableStream
4748
* @param {Object} options
49+
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
50+
* {String} options.callback.url the OSS sends a callback request to this URL
51+
* {String} options.callback.host The host header value for initiating callback requests
52+
* {String} options.callback.body The value of the request body when a callback is initiated
53+
* {String} options.callback.contentType The Content-Type of the callback requests initiatiated
54+
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
55+
* customValue = {
56+
* key1: 'value1',
57+
* key2: 'value2'
58+
* }
4859
* @return {Object}
4960
*/
5061
proto.put = function* put(name, file, options) {
@@ -73,6 +84,8 @@ proto.put = function* put(name, file, options) {
7384
params.content = content;
7485
params.successStatuses = [200];
7586

87+
callback.encodeCallback(options);
88+
7689
var result = yield this.request(params);
7790

7891
var ret = {
@@ -113,6 +126,8 @@ proto.putStream = function* putStream(name, stream, options) {
113126
params.stream = stream;
114127
params.successStatuses = [200];
115128

129+
callback.encodeCallback(options);
130+
116131
var result = yield this.request(params);
117132

118133
var ret = {

lib/common/callback.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
4+
exports.encodeCallback = function(options) {
5+
options.headers = options.headers || {};
6+
if (!options.headers.hasOwnProperty('x-oss-callback')) {
7+
if (options.callback) {
8+
var json = {
9+
callbackUrl: encodeURI(options.callback.url),
10+
callbackBody: options.callback.body
11+
};
12+
if (options.callback.host) {
13+
json.callbackHost = options.callback.host;
14+
}
15+
if (options.callback.contentType) {
16+
json.callbackBodyType = options.callback.contentType;
17+
}
18+
var callback = new Buffer(JSON.stringify(json)).toString('base64');
19+
options.headers['x-oss-callback'] = callback;
20+
21+
if (options.callback.customValue) {
22+
var callbackVar = {};
23+
for (var key in options.callback.customValue) {
24+
callbackVar['x:' + key] = options.callback.customValue[key];
25+
}
26+
options.headers['x-oss-callback-var'] = new Buffer(JSON.stringify(callbackVar)).toString('base64');
27+
}
28+
}
29+
}
30+
};

lib/common/multipart-copy.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,21 @@ proto._resumeMultipartCopy = function* _resumeMultipartCopy(checkpoint, sourceDa
113113
var partOffs = this._divideMultipartCopyParts(copySize, partSize, sourceData.startOffset);
114114
var numParts = partOffs.length;
115115

116+
var uploadPartCopyOptions = {
117+
headers:{}
118+
};
119+
120+
if (options.copyheaders) {
121+
copy(options.copyheaders).to(uploadPartCopyOptions.headers);
122+
}
123+
116124
var uploadPartJob = function* (self, partNo, sourceData) {
117125
if (!self.isCancel()) {
118126
try {
119127
var pi = partOffs[partNo - 1];
120128
var range = pi.start + '-' + (pi.end - 1);
121129

122-
if (options.copyheaders) {
123-
copy(options.copyheaders).to(options.headers);
124-
}
125-
126-
var result = yield self.uploadPartCopy(name, uploadId, partNo, range, sourceData, options);
130+
var result = yield self.uploadPartCopy(name, uploadId, partNo, range, sourceData, uploadPartCopyOptions);
127131

128132
if (!self.isCancel()) {
129133
debug('content-range ' + result.res.headers['content-range']);

lib/common/multipart.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
2+
var callback = require('./callback');
33

44
var proto = exports;
55

@@ -149,6 +149,16 @@ proto.uploadPart = function* uploadPart(name, uploadId, partNo, file, start, end
149149
* {Integer} number partNo
150150
* {String} etag part etag uploadPartCopy result.res.header.etag
151151
* @param {Object} options
152+
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
153+
* {String} options.callback.url the OSS sends a callback request to this URL
154+
* {String} options.callback.host The host header value for initiating callback requests
155+
* {String} options.callback.body The value of the request body when a callback is initiated
156+
* {String} options.callback.contentType The Content-Type of the callback requests initiatiated
157+
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
158+
* customValue = {
159+
* key1: 'value1',
160+
* key2: 'value2'
161+
* }
152162
*/
153163
proto.completeMultipartUpload = function* completeMultipartUpload(name, uploadId, parts, options) {
154164
parts.sort((a, b) => a.number - b.number);
@@ -167,6 +177,9 @@ proto.completeMultipartUpload = function* completeMultipartUpload(name, uploadId
167177
var params = this._objectRequestParams('POST', name, options);
168178
params.mime = 'xml';
169179
params.content = xml;
180+
181+
callback.encodeCallback(options);
182+
170183
if (!(options.headers && options.headers['x-oss-callback'])) {
171184
params.xmlResponse = true;
172185
}

lib/managed_upload.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ var proto = exports;
1919
* @param {String} name
2020
* @param {String|File} file
2121
* @param {Object} options
22+
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
23+
* {String} options.callback.url the OSS sends a callback request to this URL
24+
* {String} options.callback.host The host header value for initiating callback requests
25+
* {String} options.callback.body The value of the request body when a callback is initiated
26+
* {String} options.callback.contentType The Content-Type of the callback requests initiatiated
27+
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
28+
* customValue = {
29+
* key1: 'value1',
30+
* key2: 'value2'
31+
* }
2232
*/
2333
proto.multipartUpload = function* multipartUpload(name, file, options) {
2434
options = options || {};
@@ -104,7 +114,7 @@ proto._resumeMultipart = function* _resumeMultipart(checkpoint, options) {
104114
size: pi.end - pi.start
105115
};
106116

107-
var result = yield self._uploadPart(name, uploadId, partNo, data, options);
117+
var result = yield self._uploadPart(name, uploadId, partNo, data);
108118
doneParts.push({
109119
number: partNo,
110120
etag: result.res.headers.etag

lib/object.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var copy = require('copy-to');
1212
var querystring = require('querystring');
1313
var path = require('path');
1414
var mime = require('mime');
15+
var callback = require('./common/callback');
1516
var assert = require('assert');
1617

1718

@@ -47,12 +48,23 @@ proto.append = function* (name, file, options) {
4748
* @param {String} name the object key
4849
* @param {Mixed} file String(file path)/Buffer/ReadableStream
4950
* @param {Object} options
51+
* {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
52+
* {String} options.callback.url the OSS sends a callback request to this URL
53+
* {String} options.callback.host The host header value for initiating callback requests
54+
* {String} options.callback.body The value of the request body when a callback is initiated
55+
* {String} options.callback.contentType The Content-Type of the callback requests initiatiated
56+
* {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
57+
* customValue = {
58+
* key1: 'value1',
59+
* key2: 'value2'
60+
* }
5061
* @return {Object}
5162
*/
5263
proto.put = function* put(name, file, options) {
5364
var content;
5465

5566
options = options || {};
67+
5668
if (is.buffer(file)) {
5769
content = file;
5870
} else if (is.string(file)) {
@@ -69,6 +81,8 @@ proto.put = function* put(name, file, options) {
6981
options.headers = options.headers || {};
7082
this._convertMetaToHeaders(options.meta, options.headers);
7183

84+
callback.encodeCallback(options);
85+
7286
var method = options.method || 'PUT';
7387
var params = this._objectRequestParams(method, name, options);
7488
params.mime = options.mime;
@@ -108,6 +122,8 @@ proto.putStream = function* putStream(name, stream, options) {
108122
}
109123
this._convertMetaToHeaders(options.meta, options.headers);
110124

125+
callback.encodeCallback(options);
126+
111127
var method = options.method || 'PUT';
112128
var params = this._objectRequestParams(method, name, options);
113129

0 commit comments

Comments
 (0)