All Products
Search
Document Center

Object Storage Service:Upload files (Node.js SDK)

Last Updated:Nov 29, 2025

This topic describes how to upload files (objects) to a versioning-enabled bucket.

Simple upload

If you initiate a request to upload an object to a versioning-enabled bucket, OSS generates a unique version ID for the uploaded object and includes the version ID in the response as the value of the x-oss-version-id header. If you upload an object to a versioning-suspended bucket, OSS generates a version ID of null for the object. If the object that you upload has the same name as an existing object, the existing object is overwritten by the uploaded object. This way, each object has only a version whose version ID is null.

The following code provides an example on how to upload an object to a versioned bucket by using simple upload:

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

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this code, make sure 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 bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function put() {
  const result = await client.put('fileName', path.normalize('D:\\localpath\\examplefile.txt'));
  console.log(result.res.headers['x-oss-version-id']); // View the version ID of the uploaded object.
}
put();

Append upload

In a versioning-enabled bucket, the AppendObject operation can be performed only on the current version of an appendable object.

Note
  • When you perform the AppendObject operation on the current version of an appendable object, OSS does not generate a previous version for the object.

  • When you perform the PutObject or DeleteObject operation on an object whose current version is an appendable object, OSS stores the appendable object as a previous version and prevents the object from being further appended.

  • The AppendObject operation cannot be performed on an object whose current version is a non-appendable object, such as a normal object or a delete marker.

The following code provides an example on how to upload an object to a versioned bucket by using append upload:

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

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this code, make sure 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 bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function append() {
  await client.append('filename', path.normalize('D:\\localpath\\examplefile.txt'), {
    partSize: 100 * 1024
  });
}
append();

Multipart upload

In a versioning-enabled bucket, you can call the multipartUpload method to upload a file in parts. After the multipart upload is complete, OSS generates a unique version ID for the entire file and returns the ID in the x-oss-version-id response header.

The following code provides an example on how to upload an object to a versioned bucket by using multipart upload:

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

const client = new OSS({
  // Set region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set region to oss-cn-hangzhou.
  region: 'yourregion',
  // Obtain access credentials from environment variables. Before running this code, make sure 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 bucket to the name of your bucket.
  bucket: 'yourbucketname'
});

async function multipartUpload() {
  const result = await client.multipartUpload('filename', path.normalize('D:\\localpath\\examplefile.txt'), {
    partSize: 100 * 1024
  });
  // View the version ID of the uploaded object.
  console.log(result.res.headers['x-oss-version-id']); 
}
multipartUpload();

References