Help Center> Object Storage Service> Node.js> Object Upload> Performing a Streaming Upload
Updated on 2023-11-09 GMT+08:00

Performing a Streaming Upload

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference.

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

Uploading a Network Stream

// Import the OBS library.
// Use npm to install the client.
const ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// const ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

// Define the network stream URL.
const url = 'https://www.example.com'
// Import HTTP and HTTPS libraries.
const http = require('http');
const https = require('https');
// Choose the corresponding library based on the URL.
const request = url.startsWith('http') ? http : https;
// Obtain the network stream.
request.get(url, (res) => {
    if (res.statusCode === 200) {
        // Call the PutObject method after the network stream is successfully obtained.
        obsClient.putObject({
            // Bucket name, for example, examplebucket.
            Bucket: 'examplebucket',
            // Enter a complete object path, for example, exampledir/exampleobject.txt. This path cannot contain the bucket name.
            Key: 'exampledir/exampleobject.txt',
            // res is an instance of the http.IncomingMessage class and is a readable stream.
            Body: res
        }, (err, result) => {
            if (err) {
                console.error('Error-->' + err);
                // Handle network exceptions or other exceptions.
            } else {
                console.log('Status-->' + result.CommonMsg.Status);
                // Process the service code.
            }
        });
    }
});

Uploading a File Stream

// Import the OBS library.
// Use npm to install the client.
const ObsClient = require('esdk-obs-nodejs');
// Use the source code to install the client.
// const ObsClient = require('./lib/obs');

// Create an ObsClient instance.
var obsClient = new ObsClient({
       //Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage.
       //Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
       access_key_id: process.env.ACCESS_KEY_ID,
       secret_access_key: process.env.SECRET_ACCESS_KEY,
       server : 'https://your-endpoint'
});

const fs = require('fs');
// Enter the complete path of the local file and read the data stream from the local file.
// If there is no local path in the complete path, the system uploads the file from the local path of the project where the sample program is located by default.
const stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
obsClient.putObject({
     // Bucket name, for example, examplebucket.
     Bucket: 'examplebucket',
     // Enter a complete object path, for example, exampledir/exampleobject.txt. This path cannot contain the bucket name.
     Key: 'exampledir/exampleobject.txt',
     // stream is a file readable stream.
     Body: stream
}, (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.
  • The content to be uploaded cannot exceed 5 GB.