The versioning state of a bucket applies to all objects in the bucket. If you enable versioning for a bucket, you can recover any previous version of an object in the bucket when you accidentally overwrite or delete the object.

If you enable versioning for a bucket, you can recover any previous version of an object in the bucket when you accidentally overwrite or delete the object. A bucket can be in 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({
  // 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',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname'
});

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('BucketName', status);
  console.log(result);
}
putBucketVersioning();

Query 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({
  // 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',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname'
});

async function getBucketVersioning() {
  // Query the versioning state of the bucket. 
  const result = await client.getBucketVersioning('BucketName');
  console.log(result.versionStatus);
}
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({
  // 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',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Set yourbucketname to the name of your bucket. 
  bucket: 'yourbucketname'
});

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();

References

  • For the complete sample code that is used to configure versioning for a bucket, visit GitHub.
  • For more information about the API operation that you can call to configure versioning for a bucket, see PutBucketVersioning.
  • For more information about the API operation that you can call to query the versioning state of a bucket, see GetBucketVersioning.
  • For more information about the API operation that you can call to list the versions of all objects including delete markers in a bucket, see GetBucketVersions(ListObjectVersions).