Le versioning s'applique à tous les objets d'un bucket.
Notes
Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.
Cette rubrique explique comment créer une instance
OSSClientavec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.Pour définir l'état du versioning d'un bucket, vous devez disposer de l'autorisation
oss:PutBucketVersioning. Pour récupérer les informations relatives à l'état du versioning d'un bucket, vous devez disposer de l'autorisationoss:GetBucketVersioning. Pour plus d'informations, consultez Accorder une politique d'accès personnalisée à un utilisateur RAM.
Définir l'état du versioning d'un bucket
Le code suivant montre comment activer ou suspendre le versioning pour un bucket.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Create a bucket versioning configuration and set the status to Enabled or Suspended. */
SetBucketVersioningRequest setrequest(BucketName, VersioningStatus::Enabled);
auto outcome = client.SetBucketVersioning(setrequest);
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "SetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Récupérer l'état du versioning d'un bucket
Le code suivant montre comment récupérer l'état du versioning d'un bucket.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get the versioning state of the bucket. */
auto outcome = client.GetBucketVersioning(GetBucketVersioningRequest(BucketName));
if (!outcome.isSuccess()) {
/* Handle the exception. */
std::cout << "GetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Lister toutes les versions d'objets dans un bucket
Le code suivant montre comment lister toutes les versions d'objets, y compris les marqueurs de suppression, dans un bucket spécifié.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize OSS account information. */
/* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name, for example, examplebucket. */
std::string BucketName = "examplebucket";
/* Specify the full path of the object. The full path cannot contain the bucket name. Example: exampledir/exampleobject.txt. */
std::string ObjectName = "exampledir/exampleobject.txt";
/* Initialize network resources. */
InitializeSdk();
ClientConfiguration conf;
conf.signatureVersion = SignatureVersionType::V4;
/* Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
ListObjectVersionsRequest request(BucketName);
bool IsTruncated = false;
do {
auto outcome = client.ListObjectVersions(request);
if (outcome.isSuccess()) {
/* View the version information of the listed object 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 the 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;
}
Références
Pour plus d'informations sur l'opération API permettant de définir l'état du versioning d'un bucket, consultez PutBucketVersioning.
Pour plus d'informations sur l'opération API permettant de récupérer l'état du versioning d'un bucket, consultez GetBucketVersioning.