Skip to content

Commit 50f0093

Browse files
duan007aPeterRao
authored andcommitted
fix(Browser): signatureUrl with content-type and content-md5 (#441)
* fix: fix signatureUrl's logic without content-type and content-md5 * fix: fix configuration about browser's test * test: add testcase and sample about signatureUrl for posting data to server * test: restore configurations * test: rename id and function * test: revise callback logic * test: fix testcases
1 parent e22ecf6 commit 50f0093

File tree

6 files changed

+95
-10
lines changed

6 files changed

+95
-10
lines changed

example/index.html

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,29 @@ <h1>OSS <small>in</small> Browser</h1>
9191
</div>
9292
</div>
9393
</td>
94+
95+
<td>
96+
<div class="panel panel-success">
97+
<div class="panel-heading">PUT Object with signaturedUrl</div>
98+
<div class="panel-body">
99+
<form action="" class="form-horizontal">
100+
<div class="form-group">
101+
<label>Content</label>
102+
<textarea class="form-control" id="put-blob" rows="3">Hello, OSS! I am a Blob!</textarea>
103+
</div>
104+
<div class="form-group">
105+
<label>Store as</label>
106+
<input type="text" class="form-control" id="object-key-put-blob" value="blob" />
107+
</div>
108+
<div class="form-group">
109+
<input type="button" class="btn btn-primary" id="put-blob-button" value="Save" />
110+
</div>
111+
</form>
112+
</div>
113+
</div>
114+
</td>
115+
</tr>
116+
<tr>
94117
<td>
95118
<div class="panel panel-danger">
96119
<div class="panel-heading">Upload with base64 img</div>
@@ -123,8 +146,6 @@ <h1>OSS <small>in</small> Browser</h1>
123146
</div>
124147
</div>
125148
</td>
126-
</tr>
127-
<tr>
128149
<td>
129150
<div class="panel panel-warning">
130151
<div class="panel-heading">Download file</div>
@@ -145,6 +166,8 @@ <h1>OSS <small>in</small> Browser</h1>
145166
</div>
146167
</div>
147168
</td>
169+
</tr>
170+
<tr>
148171
<td>
149172
<div class="panel panel-info">
150173
<div class="panel-heading">List files</div>
@@ -160,8 +183,6 @@ <h1>OSS <small>in</small> Browser</h1>
160183
</div>
161184
</div>
162185
</td>
163-
</tr>
164-
<tr>
165186
<td colspan="2">
166187
Powered by
167188
<a href="https://www.aliyun.com/product/oss">OSS</a> &

example/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const $ = require('jquery');
66
// if use in react , you can use require('ali-oss/dist/aliyun-oss-sdk.js'), or see webpack.prod.js
77
const OSS = require('ali-oss');
8+
const crypto = require('crypto');
89

910
const appServer = '/sts';
1011
const bucket = '<bucket-name>';
@@ -183,6 +184,32 @@ const uploadBlob = function (client) {
183184
return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client));
184185
};
185186

187+
const putBlob = function (client) {
188+
const content = document.getElementById('put-blob').value.trim();
189+
const key = document.getElementById('object-key-put-blob').value.trim() || 'blob';
190+
const md5String = crypto.createHash('md5').update(new Buffer(content, 'utf8')).digest('base64');
191+
const options = {
192+
expires: 1800,
193+
method: 'PUT',
194+
'Content-Type': 'text/plain; charset=UTF-8',
195+
'Content-Md5': md5String,
196+
};
197+
const url = client.signatureUrl(key, options);
198+
199+
return $.ajax({
200+
url,
201+
method: 'PUT',
202+
data: content,
203+
beforeSend(xhr) {
204+
xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8');
205+
xhr.setRequestHeader('Content-MD5', md5String);
206+
},
207+
crossDomain: true,
208+
complete(jqXHR, textStatus) {
209+
console.log(textStatus);
210+
},
211+
});
212+
};
186213

187214
const downloadFile = function (client) {
188215
const object = document.getElementById('dl-object-key').value.trim();
@@ -222,6 +249,10 @@ window.onload = function () {
222249
applyTokenDo(uploadBlob);
223250
};
224251

252+
document.getElementById('put-blob-button').onclick = function () {
253+
applyTokenDo(putBlob);
254+
};
255+
225256
document.getElementById('list-files-button').onclick = function () {
226257
applyTokenDo(listFiles);
227258
};

lib/common/signUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r
124124
const value = options[key];
125125
if (lowerKey.indexOf('x-oss-') === 0) {
126126
headers[lowerKey] = value;
127+
} else if (lowerKey.indexOf('content-md5') === 0) {
128+
headers[key] = value;
129+
} else if (lowerKey.indexOf('content-type') === 0) {
130+
headers[key] = value;
127131
} else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method') {
128132
subResource[lowerKey] = value;
129133
}

task/browser-test-build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function build(options, callback) {
5858
aliases: {
5959
'zlib': false,
6060
'iconv-lite': false,
61-
'crypto': './shims/crypto.js',
61+
'crypto': './shims/crypto/crypto.js',
6262
},
6363
verbose: false
6464
}).bundle(function(err, data) {

test/browser/browser.test.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { callbackServer } = require('../../test/const');
1616
const { prefix } = utisl;
1717
const sinon = require('sinon');
1818
const md5 = require('crypto-js/md5');
19-
19+
const crypto1 = require('crypto');
2020

2121
let ossConfig;
2222
const timemachine = require('timemachine');
@@ -551,9 +551,25 @@ describe('browser', () => {
551551
// });
552552
//
553553
it('should signature url for PUT', function* () {
554-
const url = this.store.signatureUrl(this.name, { method: 'PUT' });
555-
const res = yield urllib.request(url, { method: 'PUT' });
554+
const putString = 'Hello World';
555+
const contentMd5 = crypto1
556+
.createHash('md5')
557+
.update(new Buffer(putString, 'utf8'))
558+
.digest('base64');
559+
console.log(contentMd5);
560+
const url = this.store.signatureUrl(this.name, {
561+
method: 'PUT',
562+
'Content-Type': 'text/plain; charset=UTF-8',
563+
'Content-Md5': contentMd5,
564+
});
565+
const headers = {
566+
'Content-Type': 'text/plain; charset=UTF-8',
567+
'Content-MD5': contentMd5,
568+
};
569+
const res = yield urllib.request(url, { method: 'PUT', data: putString, headers });
556570
assert.equal(res.status, 200);
571+
const headRes = yield this.store.head(this.name);
572+
assert.equal(headRes.status, 200);
557573
});
558574

559575
it('should signature url get need escape object ok', function* () {

test/node/object.test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const urllib = require('urllib');
1313
const copy = require('copy-to');
1414
const mm = require('mm');
1515
const streamEqual = require('stream-equal');
16+
const crypto = require('crypto');
1617

1718
const tmpdir = path.join(__dirname, '.tmp');
1819
if (!fs.existsSync(tmpdir)) {
@@ -786,9 +787,21 @@ describe('test/object.test.js', () => {
786787
});
787788

788789
it('should signature url for PUT', function* () {
789-
const url = this.store.signatureUrl(this.name, { method: 'PUT' });
790-
const res = yield urllib.request(url, { method: 'PUT' });
790+
const putString = 'Hello World';
791+
const contentMd5 = crypto.createHash('md5').update(new Buffer(putString, 'utf8')).digest('base64');
792+
const url = this.store.signatureUrl(this.name, {
793+
method: 'PUT',
794+
'Content-Type': 'text/plain; charset=UTF-8',
795+
'Content-Md5': contentMd5,
796+
});
797+
const headers = {
798+
'Content-Type': 'text/plain; charset=UTF-8',
799+
'Content-MD5': contentMd5,
800+
};
801+
const res = yield urllib.request(url, { method: 'PUT', data: putString, headers });
791802
assert.equal(res.status, 200);
803+
const headRes = yield this.store.head(this.name);
804+
assert.equal(headRes.status, 200);
792805
});
793806

794807
it('should signature url for PUT with callback parameter', function* () {

0 commit comments

Comments
 (0)