A bucket is a container for objects stored in OSS. Every object is contained in a bucket. This topic describes how to create a bucket.
The following code provides an example on how to create a bucket:
#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* Use a char* string to initialize the aos_string_t data type. */
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* Specify whether to use CNAME. 0 indicates that the CNAME is not used. */
options->config->is_cname = 0;
/* Configure network parameters, such as timeout. */
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* Call aos_http_io_initialize in main() to initialize global resources such as the network and memory. */
if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
exit(1);
}
/* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
aos_pool_t *pool;
/* Create a new memory pool. The second parameter is NULL. This value indicates that the pool does not inherit any other memory pool. */
aos_pool_create(&pool, NULL);
/* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
oss_request_options_t *oss_client_options;
/* Allocate a memory chunk in the memory pool to options. */
oss_client_options = oss_request_options_create(pool);
/* Initialize oss_client_options. */
init_options(oss_client_options);
/* Initialize parameters. */
aos_string_t bucket;
oss_acl_e oss_acl = OSS_ACL_PRIVATE;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
/* Assign char* data to the aos_string_t bucket. */ */
aos_str_set(&bucket, bucket_name);
// Create the bucket. */
resp_status = oss_create_bucket(oss_client_options, &bucket, oss_acl, &resp_headers);
/* Determine whether the bucket is created. */ */
if (aos_status_is_ok(resp_status)) {
printf("create bucket succeeded\n");
} else {
printf("create bucket failed\n");
}
/* Release the memory pool. The memory allocated to various resources used for the request is released. */
aos_pool_destroy(pool);
/* Release the allocated global resources. */
aos_http_io_deinitialize();
return 0;
}
For more information about the bucket naming conventions, see Bucket.
You can specify the ACL and storage class when you create a bucket. For more information, see Set the ACL for a bucket and Overview of storage classes.
The following code provides an example on how to set the ACL of a bucket when creating the bucket:
/* Create a bucket and set the ACL of the bucket to public read (the default ACL is private). */
resp_status = oss_create_bucket(oss_client_options, &bucket, OSS_ACL_PUBLIC_READ, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("create bucket succeeded\n");
} else {
printf("create bucket failed\n");
}
The following code provides an example on how to create a bucket of the Infrequent Access (IA) storage class.
To create a bucket of the Archive storage class, replace OSS_STORAGE_CLASS_IA in the following example with OSS_STORAGE_CLASS_ARCHIVE.
resp_status = oss_create_bucket_with_storage_class(oss_client_options, &bucket, OSS_ACL_PRIVATE, OSS_STORAGE_CLASS_IA);
/* Determine whether the bucket of the specified storage class is created. */ */
if (aos_status_is_ok(resp_status)) {
printf("create bucket succeeded\n");
} else {
printf("create bucket failed\n");
}
For more information about how to create a bucket, see PutBucket.