All Products
Search
Document Center

Object Storage Service:Download a file (Node.js SDK)

Last Updated:Mar 20, 2026

Use the GetObject operation to download an object from a bucket. By default, GetObject on a versioning-enabled bucket returns the current version of the object.

How it works

The response depends on the state of the object:

  • If the current version is a delete marker, OSS returns 404 Not Found.

  • If you specify a version ID, OSS returns that version. If the version ID is null, OSS returns the version whose version ID is null.

  • If the specified version ID belongs to a delete marker, OSS returns 405 Method Not Allowed.

Prerequisites

Before you begin, make sure you have:

  • An OSS bucket

  • The oss:GetObject permission. To download a specific version, you also need oss:GetObjectVersion. If the object metadata contains X-Oss-Server-Side-Encryption: KMS, you also need kms:Decrypt.

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant permissions through a RAM Policy or bucket policy.

Download an object

The following example downloads an object and reads its content.

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

const client = new OSS({
  // Replace <your-region> with the region where your bucket is located,
  // for example, oss-cn-hangzhou for the China (Hangzhou) region.
  region: '<your-region>',
  // Obtain 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,
  // Replace <your-bucket-name> with your bucket name.
  bucket: '<your-bucket-name>',
});

async function downloadObject() {
  // Replace <object-name> with the name of the object to download.
  const result = await client.get('<object-name>');
  console.log(result.content);
}

downloadObject();

Download a specific version of an object

To download a specific version, pass versionId to the get method.

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

const client = new OSS({
  region: '<your-region>',
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  bucket: '<your-bucket-name>',
});

async function downloadObjectVersion() {
  // Replace <object-name> and <version-id> with the target values.
  const result = await client.get('<object-name>', {
    versionId: '<version-id>',
  });
  console.log(result.content);
}

downloadObjectVersion();

Replace the following placeholders:

PlaceholderDescriptionExample
<your-region>Region where your bucket is locatedoss-cn-hangzhou
<your-bucket-name>Your bucket namemy-bucket
<object-name>Name of the object to downloadimages/photo.jpg
<version-id>Version ID of the objectCAEQHhiBgMDMtYWU1YTU5NjliZDliMzA4

References