Skip to content

Commit

Permalink
Merge pull request #3 from zengjialuo/master
Browse files Browse the repository at this point in the history
Add realUpload method to simply upload data
  • Loading branch information
leeight committed Jun 24, 2014
2 parents 499381b + e7e0d92 commit 08468a0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/sdk.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* lib/sdk.js ~ 2013/05/11 19:30:50
* @author leeight([email protected])
* zengjialuo([email protected])
* @ignore
* 封装bcs的api
**/
Expand Down Expand Up @@ -270,21 +271,51 @@ BaiduCloudStorage.prototype.upload = function(bucketName,
}

var objectName = this._getObjectName( localFile, prefix );
var data = fs.readFileSync( localFile );

var realUploadDef = this.realUpload( data, bucketName, objectName);
realUploadDef.done(function ( url ) {
def.resolve( url );
});
realUploadDef.fail(function (e) {
def.reject( e );
});

return def;
};

/**
* @param {string | Buffer} data 上传文件的内容
* @param {string} bucketName The Bucket Name
* @param {string=} objectName the Object Name
*
* @return {edp.Deferred}
*/
BaiduCloudStorage.prototype.realUpload = function(data,
bucketName,
objectName) {
var def = new edp.Deferred();

objectName = '/' + objectName;
objectName = objectName.replace(/\/+/g, '/');

var targetUrl = this.sign( 'PUT', bucketName, objectName );

var data = fs.readFileSync( localFile );
var options = require( 'url' ).parse( targetUrl );
options.method = 'PUT';
options.headers = {
'Connection': 'close',
'Content-Length': data.length
'Content-Length': Buffer.isBuffer( data )
? data.length
: Buffer.byteLength( data )
};

this._sendRequest( options, data, targetUrl, def );

return def;
};


/**
* @ignore
*/
Expand Down
39 changes: 39 additions & 0 deletions test/sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


var sdk = require('../index');
var path = require('path');


sdk.start([path.join(__dirname, './data/a.txt'), 'bs://weigou-baidu-com/hello/world'], {});


sdk.start([path.join(__dirname, './data/z/y/3.txt'), 'bs://weigou-baidu-com/'], {});


sdk.start(
[
path.join(__dirname, './data/z/y/3.txt'),
'bs://weigou-baidu-com/'
],
{
'auto-uri': true
}
);


sdk.start([path.join(__dirname, './data/z'), 'bs://weigou-baidu-com/test/upload/folder'], {});


0 comments on commit 08468a0

Please sign in to comment.