• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Object Upload
  5. Performing a Streaming Upload

Performing a Streaming Upload

Streaming upload uses stream.Readable as the data source of objects. Sample code is as follows:

Uploading a Network Stream

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

var http = require('http');

// Create a network stream.
http.get('http://www.a.com', (res) => {
       obsClient.putObject({
              Bucket : 'bucketname',
              Key : 'objectkey',
              Body : res
       }, (err, result) => {
              if(err){
                     console.error('Error-->' + err);
              }else{
                     console.log('Status-->' + result.CommonMsg.Status);
              }
       });
});

Uploading a File Stream

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

var fs = require('fs');

obsClient.putObject({
       Bucket : 'bucketname',
       Key : 'objectkey',
// Create a file stream.
       Body : fs.createReadStream('localfile')
}, (err, result) => {
       if(err){
              console.error('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});
  • When you use the Body parameter to specify the stream data to be uploaded, the parameter value must be an instance of stream.Readable.
  • To upload a large file, you are advised to use multipart upload.