All Products
Search
Document Center

Object Storage Service:Streaming Upload (Node.js SDK)

Last Updated:Dec 23, 2025

Use streaming upload to upload data streams, such as file or network streams, to an object in a bucket.

Upload a file stream

This example uploads a file stream to an object named exampleobject.txt in the exampledir directory of the examplebucket bucket.

const OSS = require('ali-oss');
const fs = require('fs');
const client = new OSS({
  // Specify the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name, for example, examplebucket.
  bucket: 'examplebucket',
});

async function putStream () {
  try {
    // By default, the SDK uses chunked encoding and sends an HTTP PUT request when you call the putStream operation.
    // Specify the full path of the local source file.
    // If you omit the local path, the file is uploaded from the script's directory.
    let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
    // Specify the full path of the object, for example, 'exampledir/exampleobject.txt'. The full path cannot contain the bucket name.
    let result = await client.putStream('exampledir/exampleobject.txt', stream);    

    // To disable chunked encoding, specify the contentLength parameter in the options.
    // let stream = fs.createReadStream('D:\\localpath\\examplefile.txt');
    // let size = fs.statSync('D:\\localpath\\examplefile.txt').size;
    // let result = await client.putStream(
    // The stream parameter can be any Readable Stream object, such as a file stream or a network stream.
    // 'exampledir/exampleobject.txt', stream, {contentLength: size}); 
    console.log(result); 
  } catch (e) {
    console.log(e)
  }
}

putStream();

Upload a network stream

This example uploads a network stream to an object named example.png in the examplebucket bucket.

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

const client = new OSS({  
  // Specify the region where the bucket is located. For example, for China (Hangzhou), set region to oss-cn-hangzhou.
  region: 'yourRegion',
  // Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name, for example, examplebucket.
  bucket: 'examplebucket',
});

// Specify the URL of the network stream.
const url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/en-US/20220908/cbgh/image_processing_example.jpg";
// Import the Duplex stream.
// The stream parameter can be any Readable Stream object, such as a file stream or a network stream.
const Duplex = require("stream").Duplex;
// Instantiate a Duplex stream.
let stream = new Duplex();

urllib.request(url, (err, data, res) => {
  if (!err) {
    // Push data into the Duplex stream.
    stream.push(data);
    stream.push(null);

    client
      // Specify the full path of the object, for example, 'example.png'. The full path cannot contain the bucket name.
      .putStream("example.png", stream)
      .then((r) => console.log(r))
      .catch((e) => console.log(e));
  }
});

References

See the complete sample code for streaming upload on GitHub.