All Products
Search
Document Center

Object Storage Service:Upload from the local memory using OSS SDK for Node.js

Last Updated:Mar 20, 2026

Upload in-memory data directly to OSS—without writing to a local file first—using the put method of the ali-oss Node.js SDK.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket

  • The OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables set with valid credentials

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

  • The required permissions (see Permissions)

Permissions

By default, an Alibaba Cloud account has full permissions. RAM users and RAM roles have no permissions by default. Grant the required permissions through RAM Policy or Bucket policies.

APIActionWhen required
PutObjectoss:PutObjectAlways required to upload an object
PutObjectoss:PutObjectTaggingRequired when specifying object tags via x-oss-tagging
PutObjectkms:GenerateDataKey, kms:DecryptRequired when the object metadata includes X-Oss-Server-Side-Encryption: KMS

Upload from local memory

The following example uploads a Buffer to OSS using the put method.

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

const client = new OSS({
  // Set region to the region where your bucket is located.
  // Example: oss-cn-hangzhou for the China (Hangzhou) region.
  region: 'oss-cn-hangzhou',
  // Read credentials from environment variables to avoid hardcoding sensitive values.
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  authorizationV4: true,
  bucket: 'examplebucket',
});

async function putBuffer() {
  try {
    const result = await client.put('exampledir/exampleobject.txt', Buffer.from('hello world'));
    console.log(result);
  } catch (e) {
    console.log(e);
  }
}

putBuffer();

Replace the placeholder values before running the example:

PlaceholderDescriptionExample
oss-cn-hangzhouThe region where your bucket is locatedoss-us-east-1
examplebucketThe name of your bucketmy-bucket
exampledir/exampleobject.txtThe full object key (path and filename) in OSSuploads/data.json

References

For the underlying API operation, see PutObject.