Versioning applies to all objects in a bucket. With versioning, you can restore an object to any of its previous versions in the bucket if it is accidentally overwritten or deleted.
A bucket can be in one of three versioning states: unversioned (default), versioning-enabled, or versioning-suspended. For more information about versioning, see Versioning.
Versioning is supported in software development kit (SDK) versions 6.8.0 and later.
Set the versioning state of a bucket
The following sample code provides an example on how to enable or suspend versioning for a bucket:
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 you run the sample 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 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();Get the versioning state of a bucket
The following sample code provides an example on how to query the versioning state of a bucket:
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 you run the sample 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 getBucketVersioning() {
// Get the versioning state of the bucket.
const result = await client.getBucketVersioning('BucketName');
console.log(result.versionStatus);
}
getBucketVersioning();List all object versions in a bucket
The following sample code provides an example on how to list the versions of all objects including delete markers in a bucket:
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 you run the sample 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 getBucketVersions() {
// List all object versions, including delete markers.
const result = await client.getBucketVersions();
console.log(result.objects);
console.log(result.deleteMarker);
}
getBucketVersions();References
For the complete sample code for managing versioning, see the GitHub example.
For information about the API operation to set the versioning state of a bucket, see PutBucketVersioning.
For information about the API operation to retrieve the versioning state of a bucket, see GetBucketVersioning.
For information about the API operation to list all object versions in a bucket, including delete markers, see ListObjectVersions (GetBucketVersions).