From fe88cd204f700b421c2ab30e5ad5e11e6b062c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Fri, 13 Apr 2018 20:03:14 +0800 Subject: [PATCH 1/7] fix: fix signatureUrl's logic without content-type and content-md5 --- lib/common/signUtils.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/common/signUtils.js b/lib/common/signUtils.js index 21beeaabe..5ae2f5bbc 100644 --- a/lib/common/signUtils.js +++ b/lib/common/signUtils.js @@ -124,6 +124,10 @@ exports._signatureForURL = function _signatureForURL(accessKeySecret, options, r const value = options[key]; if (lowerKey.indexOf('x-oss-') === 0) { headers[lowerKey] = value; + } else if (lowerKey.indexOf('content-md5') === 0) { + headers[key] = value; + } else if (lowerKey.indexOf('content-type') === 0) { + headers[key] = value; } else if (lowerKey !== 'expires' && lowerKey !== 'response' && lowerKey !== 'process' && lowerKey !== 'method') { subResource[lowerKey] = value; } From ca9e5a71779e95055b09551a4f52919d3abdd3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Fri, 13 Apr 2018 20:10:27 +0800 Subject: [PATCH 2/7] fix: fix configuration about browser's test --- task/browser-test-build.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task/browser-test-build.js b/task/browser-test-build.js index 8a68c0220..3fbdb2142 100644 --- a/task/browser-test-build.js +++ b/task/browser-test-build.js @@ -58,7 +58,7 @@ function build(options, callback) { aliases: { 'zlib': false, 'iconv-lite': false, - 'crypto': './shims/crypto.js', + 'crypto': './shims/crypto/crypto.js', }, verbose: false }).bundle(function(err, data) { From dc130b6ab892127e9fb6776dc439cad20dfca3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Fri, 13 Apr 2018 20:12:09 +0800 Subject: [PATCH 3/7] test: add testcase and sample about signatureUrl for posting data to server --- example/index.html | 29 +++++++++++++++++++++++++---- example/index.js | 34 ++++++++++++++++++++++++++++++++-- test/browser/browser.test.js | 24 ++++++++++++++++++++---- test/node/object.test.js | 17 +++++++++++++++-- 4 files changed, 92 insertions(+), 12 deletions(-) diff --git a/example/index.html b/example/index.html index f3403a3f8..def5a2d9f 100644 --- a/example/index.html +++ b/example/index.html @@ -91,6 +91,29 @@

OSS in Browser

+ + +
+
POST Object with signaturedUrl
+
+
+
+ + +
+
+ + +
+
+ +
+
+
+
+ + +
Upload with base64 img
@@ -123,8 +146,6 @@

OSS in Browser

- -
Download file
@@ -145,6 +166,8 @@

OSS in Browser

+ +
List files
@@ -160,8 +183,6 @@

OSS in Browser

- - Powered by OSS & diff --git a/example/index.js b/example/index.js index ab2b43200..b723e5c38 100644 --- a/example/index.js +++ b/example/index.js @@ -4,10 +4,11 @@ const $ = require('jquery'); // if use in react , you can use require('ali-oss/dist/aliyun-oss-sdk.js'), or see webpack.prod.js -const OSS = require('ali-oss'); +const OSS = require('..'); +const crypto = require('crypto'); const appServer = '/sts'; -const bucket = ''; +const bucket = 'aliyun-oss-js'; const region = 'oss-cn-hangzhou'; const { Buffer } = OSS; // Play without STS. NOT SAFE! Because access key id/secret are @@ -183,6 +184,31 @@ const uploadBlob = function (client) { return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client)); }; +const postBlob = function (client) { + const content = document.getElementById('post-blob').value.trim(); + const key = document.getElementById('object-key-post-blob').value.trim() || 'blob'; + const md5String = crypto.createHash('md5').update(new Buffer(content, 'utf8')).digest('base64'); + const options = { + expires: 1800, + method: 'PUT', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Md5': md5String.toString(), + }; + const url = client.signatureUrl(key, options); + + return $.ajax({ + url, + method: 'PUT', + data: content, + beforeSend: function (xhr) { + xhr.setRequestHeader('Content-Type', 'text/plain; charset=utf-8'); + xhr.setRequestHeader('Content-MD5', md5String); + }, + crossDomain: true, + }).then((res) => { + console.log(res); + }); +}; const downloadFile = function (client) { const object = document.getElementById('dl-object-key').value.trim(); @@ -222,6 +248,10 @@ window.onload = function () { applyTokenDo(uploadBlob); }; + document.getElementById('post-blob-button').onclick = function () { + applyTokenDo(postBlob); + }; + document.getElementById('list-files-button').onclick = function () { applyTokenDo(listFiles); }; diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index f1d5dc495..59af55f40 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -16,7 +16,7 @@ const { callbackServer } = require('../../test/const'); const { prefix } = utisl; const sinon = require('sinon'); const md5 = require('crypto-js/md5'); - +const crypto1 = require('crypto'); let ossConfig; const timemachine = require('timemachine'); @@ -551,9 +551,25 @@ describe('browser', () => { // }); // it('should signature url for PUT', function* () { - const url = this.store.signatureUrl(this.name, { method: 'PUT' }); - const res = yield urllib.request(url, { method: 'PUT' }); - assert.equal(res.status, 200); + const putString = 'Hello World'; + let contentMd5 = crypto1 + .createHash('md5') + .update(new Buffer(putString, 'utf8')) + .digest('base64'); + console.log(contentMd5); + const url = this.store.signatureUrl(this.name, { + method: 'PUT', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Md5': contentMd5, + }); + const headers = { + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-MD5': contentMd5, + }; + const res = yield urllib.request(url, { method: 'PUT', data: putString, headers }); + assert.equal(res.status, 200); + const headRes = yield this.store.head(this.name); + assert.equal(headRes.status, 200); }); it('should signature url get need escape object ok', function* () { diff --git a/test/node/object.test.js b/test/node/object.test.js index 79ce00a9a..efc32c110 100644 --- a/test/node/object.test.js +++ b/test/node/object.test.js @@ -13,6 +13,7 @@ const urllib = require('urllib'); const copy = require('copy-to'); const mm = require('mm'); const streamEqual = require('stream-equal'); +const crypto = require('crypto'); const tmpdir = path.join(__dirname, '.tmp'); if (!fs.existsSync(tmpdir)) { @@ -786,9 +787,21 @@ describe('test/object.test.js', () => { }); it('should signature url for PUT', function* () { - const url = this.store.signatureUrl(this.name, { method: 'PUT' }); - const res = yield urllib.request(url, { method: 'PUT' }); + const putString = 'Hello World'; + const contentMd5 = crypto.createHash('md5').update(new Buffer(putString, 'utf8')).digest('base64'); + const url = this.store.signatureUrl(this.name, { + method: 'PUT', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Md5': contentMd5, + }); + const headers = { + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-MD5': contentMd5, + }; + const res = yield urllib.request(url, { method: 'PUT', data: putString, headers }); assert.equal(res.status, 200); + const headRes = yield this.store.head(this.name); + assert.equal(headRes.status, 200); }); it('should signature url for PUT with callback parameter', function* () { From bb768475a9ac769a6063ad3094a3fdeaf613481b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Mon, 16 Apr 2018 12:57:56 +0800 Subject: [PATCH 4/7] test: restore configurations --- example/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/index.js b/example/index.js index b723e5c38..a8a42601c 100644 --- a/example/index.js +++ b/example/index.js @@ -4,11 +4,11 @@ const $ = require('jquery'); // if use in react , you can use require('ali-oss/dist/aliyun-oss-sdk.js'), or see webpack.prod.js -const OSS = require('..'); +const OSS = require('ali-oss'); const crypto = require('crypto'); const appServer = '/sts'; -const bucket = 'aliyun-oss-js'; +const bucket = ''; const region = 'oss-cn-hangzhou'; const { Buffer } = OSS; // Play without STS. NOT SAFE! Because access key id/secret are @@ -192,7 +192,7 @@ const postBlob = function (client) { expires: 1800, method: 'PUT', 'Content-Type': 'text/plain; charset=UTF-8', - 'Content-Md5': md5String.toString(), + 'Content-Md5': md5String, }; const url = client.signatureUrl(key, options); @@ -201,7 +201,7 @@ const postBlob = function (client) { method: 'PUT', data: content, beforeSend: function (xhr) { - xhr.setRequestHeader('Content-Type', 'text/plain; charset=utf-8'); + xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8'); xhr.setRequestHeader('Content-MD5', md5String); }, crossDomain: true, From 638dacd09e1085154c28610a30f5a1b893e1f5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Mon, 16 Apr 2018 13:39:20 +0800 Subject: [PATCH 5/7] test: rename id and function --- example/index.html | 8 ++++---- example/index.js | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/example/index.html b/example/index.html index def5a2d9f..a48e2b48b 100644 --- a/example/index.html +++ b/example/index.html @@ -94,19 +94,19 @@

OSS in Browser

-
POST Object with signaturedUrl
+
PUT Object with signaturedUrl
- +
- +
- +
diff --git a/example/index.js b/example/index.js index a8a42601c..8fc15151b 100644 --- a/example/index.js +++ b/example/index.js @@ -184,9 +184,9 @@ const uploadBlob = function (client) { return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client)); }; -const postBlob = function (client) { - const content = document.getElementById('post-blob').value.trim(); - const key = document.getElementById('object-key-post-blob').value.trim() || 'blob'; +const putBlob = function (client) { + const content = document.getElementById('put-blob').value.trim(); + const key = document.getElementById('object-key-put-blob').value.trim() || 'blob'; const md5String = crypto.createHash('md5').update(new Buffer(content, 'utf8')).digest('base64'); const options = { expires: 1800, @@ -248,8 +248,8 @@ window.onload = function () { applyTokenDo(uploadBlob); }; - document.getElementById('post-blob-button').onclick = function () { - applyTokenDo(postBlob); + document.getElementById('put-blob-button').onclick = function () { + applyTokenDo(putBlob); }; document.getElementById('list-files-button').onclick = function () { From 409f0794bea3bb5b6d6847e617e98e46a971f877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Mon, 16 Apr 2018 14:35:50 +0800 Subject: [PATCH 6/7] test: revise callback logic --- example/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/example/index.js b/example/index.js index 8fc15151b..6dc262855 100644 --- a/example/index.js +++ b/example/index.js @@ -200,13 +200,14 @@ const putBlob = function (client) { url, method: 'PUT', data: content, - beforeSend: function (xhr) { + beforeSend(xhr) { xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8'); xhr.setRequestHeader('Content-MD5', md5String); }, crossDomain: true, - }).then((res) => { - console.log(res); + complete(jqXHR, textStatus) { + console.log(textStatus); + }, }); }; From 03c5c6bf11f3d359c3283cd5efc7ab42cea83c66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=A6=E7=94=9F?= Date: Mon, 16 Apr 2018 16:54:05 +0800 Subject: [PATCH 7/7] test: fix testcases --- test/browser/browser.test.js | 38 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index 59af55f40..23bae639b 100644 --- a/test/browser/browser.test.js +++ b/test/browser/browser.test.js @@ -551,25 +551,25 @@ describe('browser', () => { // }); // it('should signature url for PUT', function* () { - const putString = 'Hello World'; - let contentMd5 = crypto1 - .createHash('md5') - .update(new Buffer(putString, 'utf8')) - .digest('base64'); - console.log(contentMd5); - const url = this.store.signatureUrl(this.name, { - method: 'PUT', - 'Content-Type': 'text/plain; charset=UTF-8', - 'Content-Md5': contentMd5, - }); - const headers = { - 'Content-Type': 'text/plain; charset=UTF-8', - 'Content-MD5': contentMd5, - }; - const res = yield urllib.request(url, { method: 'PUT', data: putString, headers }); - assert.equal(res.status, 200); - const headRes = yield this.store.head(this.name); - assert.equal(headRes.status, 200); + const putString = 'Hello World'; + const contentMd5 = crypto1 + .createHash('md5') + .update(new Buffer(putString, 'utf8')) + .digest('base64'); + console.log(contentMd5); + const url = this.store.signatureUrl(this.name, { + method: 'PUT', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Md5': contentMd5, + }); + const headers = { + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-MD5': contentMd5, + }; + const res = yield urllib.request(url, { method: 'PUT', data: putString, headers }); + assert.equal(res.status, 200); + const headRes = yield this.store.head(this.name); + assert.equal(headRes.status, 200); }); it('should signature url get need escape object ok', function* () {