Un bucket est un conteneur utilisé pour stocker des objets dans Object Storage Service (OSS). Cette rubrique explique comment interroger les informations relatives à 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 montre comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un nom de domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.
Pour interroger les informations d'un bucket, vous devez disposer de l'autorisation
oss:GetBucketInfo. Pour plus d'informations, consultez Accorder une politique personnalisée.
Exemple de code
Le code suivant montre comment interroger les informations d'un bucket, notamment sa région et sa date de création :
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the 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 you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. */
auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
OssClient client(Endpoint, credentialsProvider, conf);
client.SetRegion(Region);
/* Get bucket information. */
GetBucketInfoRequest request(BucketName);
auto outcome = client.GetBucketInfo(request);
/* Check the data redundancy type of the bucket. */
if (outcome.isSuccess()) {
std::cout << "GetBucketInfo success, DataRedundancyType:" << static_cast<int>(outcome.result().DataRedundancyType()) << std::endl;
}
else {
/* Handle the exception. */
std::cout << "GetBucketInfo fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Références
Pour plus d'informations sur l'opération API GetBucketInfo, consultez GetBucketInfo.