The versioning state of a bucket applies to all of the objects in the bucket.

Versioning allows you to restore objects in a bucket to any previous point in time, and protects your data from being accidentally overwritten or deleted. A bucket can be in any one of the following versioning states: unversioned (default), versioning-enabled, or versioning-suspended. For more information about versioning, see Overview in OSS Developer Guide.

Note Versioning is supported in OSS SDK for Node.js 6.8.0 or later.

Configure versioning for a bucket

The following code provides an example on how to set the versioning state of a bucket to Enabled or Suspended:

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

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function putBucketVersioning() {
  // Set the versioning state of the bucket to Enabled or Suspended.
  const status = 'Enabled'; // `Enabled` or `Suspended`
  const result = await client.putBucketVersioning('<Bucket Name>', status);
  console.log(result);
}
putBucketVersioning();

For more information about how to configure versioning for a bucket, see PutBucketVersioning.

Obtain the versioning state of a bucket

The following code provides an example on how to query information about the versioning state of a bucket:

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

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function getBucketVersioning() {
  // Obtain the versioning state of the bucket.
  const result = await client.getBucketVersioning('Bucket Name');
  console.log(result.versionStatus);
}
getBucketVersioning();

For more information about how to obtain the versioning state of a bucket, see GetBucketVersioning.

List the versions of all objects in a bucket

The following code provides an example on how to list the version information of all objects in a specified bucket, including delete markers:

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

const client = new OSS({
  bucket: '<Your BucketName>',
  // The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>'
});

async function getBucketVersions() {
  // List the versions of all objects including delete markers.
  const result = await client.getBucketVersions();
  console.log(result.objects); 
  console.log(result.deleteMarker);
}
getBucketVersions();

For more information about how to list the versions of all objects in a bucket, including delete markers, see GetBucketVersions (ListObjectVersions).