Use the OSS C++ SDK V2 to get bucket information, including basic details, region, and storage capacity.
Before you begin
-
Before you run the sample code, replace
<region>with a region, such ascn-hangzhou. -
The sample code in this topic uses environment variables for access credentials. For more information, see Configure access credentials.
-
To get basic bucket information, you must have the
oss:GetBucketInfopermission. To get the bucket region, you must have theoss:GetBucketLocationpermission. To get storage capacity information, you must have theoss:GetBucketStatpermission.
Sample code
Get basic bucket information
The following sample code retrieves basic information about a bucket:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getBucketInfo(
oss::models::GetBucketInfoRequest()
.setBucket("examplebucket"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get bucket information"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
auto& info = result.getBucketInfo();
std::cout << "Successfully got bucket information." << std::endl;
std::cout << "Name: " << info.name << std::endl;
std::cout << "Location: " << info.location << std::endl;
std::cout << "StorageClass: " << info.storageClass << std::endl;
std::cout << "CreationDate: " << info.creationDate << std::endl;
std::cout << "ExtranetEndpoint: " << info.extranetEndpoint << std::endl;
std::cout << "IntranetEndpoint: " << info.intranetEndpoint << std::endl;
return 0;
}
Get the bucket region
The following sample code retrieves the region of a bucket:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getBucketLocation(
oss::models::GetBucketLocationRequest()
.setBucket("examplebucket"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get the bucket region"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
std::cout << "Successfully got the bucket region"
<< ", LocationConstraint: " << result.getLocationConstraint() << std::endl;
return 0;
}
Get bucket storage capacity
The following sample code retrieves storage statistics for a bucket:
#include <iostream>
#include "alibabacloud/oss2/ClientConfiguration.h"
#include "alibabacloud/oss2/OSSClient.h"
#include "alibabacloud/oss2/credentials/CredentialsProvider.h"
namespace oss = alibabacloud::oss2;
int main() {
auto conf = oss::ClientConfiguration::loadDefault();
conf.region = "<region>";
conf.credentialsProvider = std::make_shared<oss::EnvironmentVariableCredentialsProvider>();
oss::OSSClient client(conf);
auto outcome = client.getBucketStat(
oss::models::GetBucketStatRequest()
.setBucket("examplebucket"));
if (!outcome.has_value()) {
auto& e = outcome.error();
std::cerr << "Failed to get bucket statistics"
<< ", code: " << e.getCode()
<< ", message: " << e.getMessage()
<< ", requestId: " << e.getRequestId() << std::endl;
return 1;
}
auto& result = outcome.value();
auto& stat = result.getBucketStat();
std::cout << "Successfully got bucket statistics." << std::endl;
std::cout << "Storage: " << stat.storage.value_or(0) << std::endl;
std::cout << "ObjectCount: " << stat.objectCount.value_or(0) << std::endl;
std::cout << "MultipartUploadCount: " << stat.multipartUploadCount.value_or(0) << std::endl;
std::cout << "StandardStorage: " << stat.standardStorage.value_or(0) << std::endl;
std::cout << "InfrequentAccessStorage: " << stat.infrequentAccessStorage.value_or(0) << std::endl;
std::cout << "ArchiveStorage: " << stat.archiveStorage.value_or(0) << std::endl;
return 0;
}
References
-
For the complete sample code to get basic information about a bucket, see GetBucketInfo.cpp.
-
For the complete sample code to get the region of a bucket, see GetBucketLocation.cpp.
-
For the complete sample code to get storage capacity information for a bucket, see GetBucketStat.cpp.