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.
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:
using Aliyun.OSS;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Initialize an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Set the versioning status of the bucket to Enabled.
client.SetBucketVersioning(new SetBucketVersioningRequest(bucketName, VersioningStatus.Enabled));;
Console.WriteLine("Create bucket Version succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Create bucket Version failed. {0}", ex.Message);
}
For more information about how to configure versioning for a bucket, see PutBucketVersioning.
Query the versioning status of a bucket
The following code provides an example on how to query information about the versioning state of a bucket:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Query the versioning status of the bucket.
var result = client.GetBucketVersioning(bucketName);
Console.WriteLine("Get bucket:{0} Version succeeded ", bucketName);
Console.WriteLine("bucket version status: {0}", result.Status);
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
For more information about how to obtain the versioning state of a bucket, see GetBucketVersioning.
List the version information 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:
using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
ObjectVersionList result = null;
var request = new ListObjectVersionsRequest(bucketName);
do {
result = client.ListObjectVersions(request);
Console.WriteLine("ListObjectVersions succeeded");
// View the version information of all listed objects.
foreach (var objectversion in result.ObjectVersionSummaries)
{
Console.WriteLine("objectversion key: {0}; objectversion versionid: {1}", objectversion.Key, objectversion.VersionId);
}
// View the version information of all listed delete markers.
foreach (var deletemarker in result.DeleteMarkerSummaries)
{
Console.WriteLine("deletemarker key: {0}; deletemarker versionid: {1}", deletemarker.Key, deletemarker.VersionId);
}
request.KeyMarker = result.NextKeyMarker;
request.NextVersionIdMarker = result.NextVersionIdMarker ;
} while (result.IsTruncated)
}
catch (OssException ex)
{
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
For more information about how to list the versions of all objects in a bucket, including delete markers, see GetBucketVersions (ListObjectVersions).