diff --git a/example/index.html b/example/index.html index f3403a3f8..a48e2b48b 100644 --- a/example/index.html +++ b/example/index.html @@ -91,6 +91,29 @@

OSS in Browser

+ + +
+
PUT 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..6dc262855 100644 --- a/example/index.js +++ b/example/index.js @@ -5,6 +5,7 @@ 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 crypto = require('crypto'); const appServer = '/sts'; const bucket = ''; @@ -183,6 +184,32 @@ const uploadBlob = function (client) { return client.put(key, new Blob([content], { type: 'text/plain' })).then(res => listFiles(client)); }; +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, + method: 'PUT', + 'Content-Type': 'text/plain; charset=UTF-8', + 'Content-Md5': md5String, + }; + const url = client.signatureUrl(key, options); + + return $.ajax({ + url, + method: 'PUT', + data: content, + beforeSend(xhr) { + xhr.setRequestHeader('Content-Type', 'text/plain; charset=UTF-8'); + xhr.setRequestHeader('Content-MD5', md5String); + }, + crossDomain: true, + complete(jqXHR, textStatus) { + console.log(textStatus); + }, + }); +}; const downloadFile = function (client) { const object = document.getElementById('dl-object-key').value.trim(); @@ -222,6 +249,10 @@ window.onload = function () { applyTokenDo(uploadBlob); }; + document.getElementById('put-blob-button').onclick = function () { + applyTokenDo(putBlob); + }; + document.getElementById('list-files-button').onclick = function () { applyTokenDo(listFiles); }; 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; } 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) { diff --git a/test/browser/browser.test.js b/test/browser/browser.test.js index f1d5dc495..23bae639b 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' }); + 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* () { 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* () {