All Products
Search
Document Center

Object Storage Service:Download objects as files using OSS SDK for Node.js

Last Updated:Mar 20, 2026

Use client.get() to download an object from a bucket to a local file.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket with at least one object

  • The ali-oss package installed (npm install ali-oss)

  • OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET set as environment variables

Store credentials in environment variables rather than hardcoding them in your source code. For production workloads, use Resource Access Management (RAM) roles instead of AccessKey credentials.

Sample code

The following example downloads exampleobject.txt from examplebucket and saves it to D:\localpath\examplefile.txt.

const OSS = require('ali-oss');

const client = new OSS({
  // Set the region where the bucket is located. For example, oss-cn-hangzhou.
  region: 'yourRegion',
  // Read credentials from environment variables.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  // Specify the bucket name.
  bucket: 'examplebucket'
});

async function get() {
  try {
    // Specify the full path of the object and the full path of the local file.
    // Do not include the bucket name in the object path.
    // If the local file exists, it is overwritten. If it does not exist, it is created.
    // If no local path is specified, the file is saved to the project directory.
    const result = await client.get('exampleobject.txt', 'D:\\localpath\\examplefile.txt');
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

get();

Parameters

client.get(name, localFilePath)

ParameterTypeDescription
namestringFull path of the object to download. Do not include the bucket name.
localFilePathstringFull path of the local file to save the object to. If the file exists, it is overwritten. If it does not exist, it is created. If omitted, the file is saved to the project directory.

What's next

  • For the complete sample code, see GitHub.

  • For the underlying API reference, see GetObject.