Call oss_get_bucket_info to retrieve metadata about a bucket, including its region, owner ID, owner name, and creation date.
Prerequisites
Before you begin, ensure that you have:
The
oss:GetBucketInfopermission. For more information, see Attach a custom policy to a RAM userAn OSSClient instance. To create one, see Initialization
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use an internal endpoint instead. For a full list of endpoints by region, see Regions and endpoints.
Store your credentials in environment variables rather than hardcoding them in your source files.
Retrieve bucket information
The sample code initializes a memory pool and client options, calls oss_get_bucket_info, and prints the returned fields. On success, the function populates an oss_bucket_info_t struct with the bucket's location, owner_id, owner_name, and created_date fields.
#include "oss_api.h"
#include "aos_http_io.h"
/* Set the endpoint for the region where your bucket is located.
Example for China (Hangzhou): https://oss-cn-hangzhou.aliyuncs.com */
const char *endpoint = "yourEndpoint";
/* Bucket name. Example: examplebucket */
const char *bucket_name = "examplebucket";
/* Set the region ID for the bucket.
Example for China (Hangzhou): cn-hangzhou */
const char *region = "yourRegion";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Initialize an aos_string_t object from a char* string. */
aos_str_set(&options->config->endpoint, endpoint);
/* Load credentials from environment variables.
Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code. */
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
/* region and signature_version are required. */
aos_str_set(&options->config->region, region);
options->config->signature_version = 4;
/* Set is_cname to 0 to use the standard OSS endpoint, not a CNAME. */
options->config->is_cname = 0;
/* Set network parameters such as timeout. */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Initialize global resources (network, memory) at program startup. */
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* Create a memory pool (equivalent to apr_pool_t; implemented in the apr library).
The second parameter is NULL, indicating this pool does not inherit from another pool. */
aos_pool_t *pool;
aos_pool_create(&pool, NULL);
/* Create and initialize the client options. */
oss_request_options_t *oss_client_options;
oss_client_options = oss_request_options_create(pool);
init_options(oss_client_options);
/* Initialize request parameters. */
aos_string_t bucket;
oss_bucket_info_t bucket_info;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_str_set(&bucket, bucket_name);
/* Retrieve bucket information. */
resp_status = oss_get_bucket_info(oss_client_options, &bucket, &bucket_info, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("get bucket info succeeded\n");
printf("location: %s\n", bucket_info.location.data);
printf("owner_id: %s\n", bucket_info.owner_id.data);
printf("owner_name: %s\n", bucket_info.owner_name.data);
printf("created_date: %s\n", bucket_info.created_date.data);
} else {
printf("get bucket info failed\n");
}
/* Release the memory pool and global resources. */
aos_pool_destroy(pool);
aos_http_io_deinitialize();
return 0;
}What's next
GetBucketInfo — the underlying API operation called by
oss_get_bucket_info