You can call the putStream operation to upload data streams such as objects and network streams to buckets in Object Storage Service (OSS). This topic describes how to use stream upload to upload a stream file.

Note You can set stream to specify the types of Readable Stream, such as file stream and network stream.

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

let OSS = require('ali-oss');
let fs = require('fs');

let 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 Resource Access Management (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, OSS SDK for Node.js 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(
    'exampledir/exampleobject.txt', stream, {contentLength: size});
    console.log(result);
  } catch (e) {
    console.log(e)
  }
}

putStream();