All Products
Search
Document Center

Object Storage Service:Versioning

Last Updated:Oct 19, 2023

The versioning setting 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: disabled (default), enabled, or suspended. For more information about versioning, see Overview

Note

Versioning is supported in Object Storage Service (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',
  // 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 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 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',
  // 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 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 versions of all objects including delete markers in a specified 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',
  // 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 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 for versioning, 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 ListObjectVersions (GetBucketVersions).