All Products
Search
Document Center

Object Storage Service:Upload from the local memory

Last Updated:Oct 18, 2023

This topic describes how to upload content from the local memory to Object Storage Service (OSS).

Sample code

This following sample code provides an example on how to upload content from the local memory to OSS.

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 putBuffer () {
  try {
    const result = await client.put('object-name', new Buffer.from('hello world'));
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

putBuffer();

References

For more information about the API operation that you can call to perform simple upload, see PutObject.