This topic describes how to upload data streams such as file streams and network streams to objects in Object Storage Service (OSS) buckets.

Upload a file stream

The following code provides an example on how to upload a file stream to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

const OSS = require('ali-oss');
const fs = require('fs');
const client = new OSS({
  // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

async function putStream () {
  try {
    // Use chunked encoding. When you call putStream, the OSS SDK initiates an HTTP PUT request for chunked encoding. 
    // Specify the full path of the local file, and data stream is read from the local file. 
    // If the path of the local file is not specified, the local file is uploaded from the path of the project to which the sample program belongs. 
    let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
    // Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
    let result = await client.putStream('exampledir/exampleobject.txt', stream);
    console.log(result);

    // Do not use chunked encoding. If contentLength is specified in options, chunked encoding is not used. 
    let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
    let size = fs.statSync('D:\\localpath\\examplefile.txt').size;
    let result = await client.putStream(
    // You can set stream to specify the types of Readable Stream, such as file stream and network stream. 
    'exampledir/exampleobject.txt', stream, {contentLength: size});
    console.log(result);
  } catch (e) {
    console.log(e)
  }
}

putStream();        

Upload a network stream

The following code provides an example on how to upload a network stream to the exampleobject.txt object in the exampledir directory of the examplebucket bucket:

const OSS = require("ali-oss");
const fs = require("fs");
const urllib = require("urllib"); 

const client = new OSS({  
  // Set yourRegion to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourRegion to oss-cn-hangzhou. 
  region: 'yourRegion',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the name of the bucket. Example: examplebucket. 
  bucket: 'examplebucket',
});

// Specify the network stream URL. 
const url = "https://gosspublic.alicdn.com/aliyun-oss-sdk-6.16.0.min.js";
// Import the duplex stream. 
// You can set stream to specify the types of Readable Stream, such as file stream and network stream. 
const Duplex = require("stream").Duplex;
// Instantiate the duplex stream. 
let stream = new Duplex();

urllib.request(url, (err, data, res) => {
  if (!err) {
    // Receive data from the duplex stream. 
    stream.push(data);
    stream.push(null);

    client
      // Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name. 
      .putStream("exampledir/exampleobject.txt", stream)
      .then((r) => console.log(r))
      .catch((e) => console.log(e));
  }
});

References

For more information about the complete sample code for stream upload, visit GitHub.