All Products
Search
Document Center

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

Last Updated:Mar 20, 2026

Streaming download retrieves an object as a Readable stream in increments rather than loading it entirely into memory. Use this approach when downloading large objects, when the download requires a long period of time to complete, or when you want to pipe the content directly to a file or another writable destination.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with the object you want to download

  • The ali-oss npm package installed

  • OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET set as environment variables

Download an object as a stream

The following example downloads exampleobject.txt from examplebucket and saves it to a local file using getStream.

Note: getStream returns a Readable stream on the result.stream property. Pipe it to any writable destination to process the file content as a stream.
const OSS = require('ali-oss');
const fs = require('fs');

const client = new OSS({
  // Set region to the region where your bucket is located.
  // Example: oss-cn-hangzhou for the China (Hangzhou) region.
  region: 'yourRegion',
  // Load credentials from environment variables.
  // Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket',
});

async function downloadAsStream() {
  try {
    // Specify the full path of the object. Do not include the bucket name.
    const result = await client.getStream('exampleobject.txt');

    // Pipe the stream to a local file.
    // If the file already exists, it is overwritten. If it does not exist, it is created.
    // If you omit the path, the file is saved to the project directory.
    const writeStream = fs.createWriteStream('D:\\localpath\\examplefile.txt');
    result.stream.pipe(writeStream);
  } catch (e) {
    console.log(e);
  }
}

downloadAsStream();

Replace the following placeholders with actual values:

PlaceholderDescriptionExample
yourRegionThe region where your bucket is locatedoss-cn-hangzhou
examplebucketThe name of your bucketmy-bucket
exampleobject.txtThe full path of the object (excluding the bucket name)logs/app.log
D:\\localpath\\examplefile.txtThe local destination path./downloads/app.log

What's next

  • For the complete sample code, see GitHub.

  • For the underlying API operation, see GetObject.