diff --git a/lib/sdk.js b/lib/sdk.js index dc3da61..13ac8a2 100644 --- a/lib/sdk.js +++ b/lib/sdk.js @@ -296,6 +296,7 @@ BaiduCloudStorage.prototype.realUpload = function(data, objectName) { var def = new edp.Deferred(); + objectName = '/' + objectName; objectName = objectName.replace(/\/+/g, '/'); var targetUrl = this.sign( 'PUT', bucketName, objectName ); diff --git a/test/sdk.spec.js b/test/sdk.spec.js index 4e66f19..1df228c 100644 --- a/test/sdk.spec.js +++ b/test/sdk.spec.js @@ -61,6 +61,45 @@ describe('sdk', function(){ }); }); + it('upload data', function(){ + var bucket = 'adtest'; + var ak = 'ak'; + var sk = 'sk'; + + var maxSize = 10 * 1024 * 1024; + var autoUri = false; + + var data = 'for upload data'; + var objectName = '/prefix/name.js'; + + var sdk = new bcs.BaiduCloudStorage( ak, sk, maxSize, autoUri ); + sdk._sendRequest = createSpy('sdk._sendRequest').andCallFake(function( options, data, targetUrl, def ){ + setTimeout(function(){ + var bcsUrl = decodeURIComponent( targetUrl.replace(/\?.*/g, '') ); + def.resolve( bcsUrl ); + }, 500); + }); + + var errorMsg = null; + var result; + var d = sdk.realUpload( data, bucket, objectName ); + d.fail(function(e){ + errorMsg = e.toString().trim(); + }); + d.done(function(x){ + result = x; + }); + + waitsFor(function(){ return d.state !== 'pending'; }); + + runs(function(){ + expect( d.state ).toBe( 'resolved' ); + expect( errorMsg ).toBe( null ); + expect( result ).toBe( 'http://bs.baidu.com/adtest/prefix/name.js' ); + }); + }); + + it('upload directory', function(){ var bucket = 'adtest'; var ak = 'ak';