All Products
Search
Document Center

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

Last Updated:Nov 29, 2025

By default, when you call the GetObject operation on an object in a versioning-enabled bucket, only the current version of the object is returned.

Background information

When you call the GetObject operation to download an object in a bucket, you may obtain one of the following results:

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

  • If a version ID of the object is specified in the request, OSS returns the specified version of the object. If the version ID is set to null in the request, OSS returns the version whose version ID is null.

  • If the version ID specified in the request is the version ID of a delete marker, OSS returns 405 Method Not Allowed.

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket policies.

API

Action

Definition

GetObject

oss:GetObject

Downloads an object.

oss:GetObjectVersion

When downloading an object, if you specify the object version through versionId, this permission is required.

kms:Decrypt

When downloading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, this permission is required.

Sample code

The following sample code shows how to download a file:

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

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

async function get() {
  // The name of the file.
  const result = await client.get('filename', {    
      // The version ID of the file to download.
      versionId: 'versionid',
  });
  console.log(result.content);
}
get();

References

For more information about the API operation used to download files, see GetObject.