All Products
Search
Document Center

Object Storage Service:Download objects to the local memory

Last Updated:Oct 19, 2023

This topic describes how to download an object to the local memory.

Sample code

The following sample code provides an example on how to download an object to the local memory:

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

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET
  // Specify the name of the bucket. 
  bucket: 'yourbucketname', 
});

async function getBuffer () {
  try {
    const result = await client.get('object-name');
    console.log(result.content);
  } catch (e) {
    console.log(e);
  }
}

getBuffer();

References

For more information about the API operation that you can call to download objects to the local memory, see GetObject.