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 Introduction to versioning in OSS Developer Guide.
Set the versioning state of a bucket
The following code provides an example on how to set the versioning state of a bucket to Enabled or Suspended:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*Initialize the OSS account information.*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*Initialize network resources.*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*Set the versioning state of the bucket to Enabled or Suspended.*/
SetBucketVersioningRequest setrequest(BucketName, VersioningStatus::Enabled);
auto outcome = client.SetBucketVersioning(setrequest);
if (! outcome.isSuccess()) {
/*Handle exceptions.*/
std::cout << "SetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*Release network resources.*/
ShutdownSdk();
return 0;
}
For more information about how to configure versioning for a bucket, see PutBucketVersioning.
Query information about the versioning state of a bucket
The following code provides an example on how to query information about the versioning state of a bucket:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*Initialize the OSS account information.*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
/*Initialize network resources.*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/*Query information about the versioning state of the bucket.*/
auto outcome = client.GetBucketVersioning(GetBucketVersioningRequest(BucketName));
if (! outcome.isSuccess()) {
/*Handle exceptions.*/
std::cout << "GetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
}
/*Release network resources.*/
ShutdownSdk();
return 0;
}
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:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/*Initialize the OSS account information.*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
std::string Endpoint = "yourEndpoint";
std::string BucketName = "yourBucketName";
std::string ObjectName = "yourObjectName";
/*Initialize network resources.*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
ListObjectVersionsRequest request(BucketName);
bool IsTruncated = false;
do {
auto outcome = client.ListObjectVersions(request);
if (outcome.isSuccess()) {
/*View the version information of listed objects whose version IDs are delete markers.*/
for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
std::cout << "marker key:" << marker.Key() << ",marker versionid:" << marker.VersionId() << std::endl;
}
/*View the version information of all listed objects.*/
for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
std::cout << "object key:" << obj.Key() << ",object versionid:" << obj.VersionId() << std::endl;
}
}
else {
std::cout << "ListObjectVersions fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
break;
}
request.setKeyMarker(outcome.result().NextKeyMarker());
request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
IsTruncated = outcome.result().IsTruncated();
} while (IsTruncated);
/*Release network resources.*/
ShutdownSdk();
return 0;
}
For more information about how to list the versions of all objects in a bucket, including delete markers, see GetBucketVersions (ListObjectVersions).