Manage a bucket

Last Updated: Aug 09, 2017

OSS uses buckets as the storage space and as the management objects for advanced features such as billing, permission control, and logging. The bucket name is globally unique throughout the OSS and cannot be changed. Every object stored on the OSS must be included in a bucket.

Create a bucket

You can create a bucket using the oss_create_bucket interface, and you must specify the bucket name:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. oss_acl_e oss_acl = OSS_ACL_PRIVATE;
  4. char *bucket_name = "<Name of your bucket>";
  5. aos_string_t bucket;
  6. aos_table_t *resp_headers;
  7. aos_status_t *s;
  8. aos_pool_create(&p, NULL);
  9. /* Create and initialize options */
  10. options = oss_request_options_create(p);
  11. init_options(options);
  12. /* Initialize parameters */
  13. aos_str_set(&bucket, bucket_name);
  14. /* Create a bucket */
  15. s = oss_create_bucket(options, &bucket, oss_acl, &resp_headers);
  16. if (aos_status_is_ok(s)) {
  17. printf("create bucket succeeded\n");
  18. } else {
  19. printf("create bucket failed\n");
  20. }
  21. aos_pool_destroy(p);

Notice:

  • For bucket naming conventions, refer to OSS Basic Concepts.
  • Because the bucket name must be globally unique, ensure that your bucket name is not already in use.

Delete a bucket

You can delete a bucket using the oss_delete_bucket interface, and you must specify the bucket name:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. char *bucket_name = "<Name of your bucket>";
  4. aos_string_t bucket;
  5. aos_table_t *resp_headers;
  6. aos_status_t *s;
  7. aos_pool_create(&p, NULL);
  8. /* Create and initialize options */
  9. options = oss_request_options_create(p);
  10. init_options(options);
  11. /* Initialize parameters */
  12. aos_str_set(&bucket, bucket_name);
  13. /* Delete a bucket */
  14. s = oss_delete_bucket (options, &bucket, &resp_headers);
  15. if (aos_status_is_ok(s)) {
  16. printf("delete bucket succeeded\n");
  17. } else {
  18. printf("delete bucket failed\n");
  19. }
  20. aos_pool_destroy(p);

Notice:

  • If the bucket to be deleted has objects in it, you need to delete the objects first.
  • If the bucket to be deleted has unfinished upload requests, you need to cancel the requests first using the oss_list_multipart_upload and oss_abort_multipart_upload interfaces.

Get the access permissions of a bucket

You can set the access permissions of a bucket to allow or prohibit anonymous reads/writes to the bucket.

Get the ACL of a bucket

You can check the ACL of a bucket through the oss_get_bucket_acl interface:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. char *bucket_name = "<Name of your bucket>";
  4. aos_string_t bucket;
  5. aos_table_t *resp_headers;
  6. aos_status_t *s;
  7. char *oss_acl;
  8. aos_pool_create(&p, NULL);
  9. /* Create and initialize options */
  10. options = oss_request_options_create(p);
  11. init_options(options);
  12. /* Initialize parameters */
  13. aos_str_set(&bucket, bucket_name);
  14. /* Get the access permissions of a bucket */
  15. s = oss_get_bucket_acl(options, &bucket,& oss_acl, &resp_headers);
  16. if (aos_status_is_ok(s)) {
  17. printf("get bucket acl succeeded\n");
  18. } else {
  19. printf("get bucket acl failed\n");
  20. }
  21. aos_pool_destroy(p);

Configure the ACL of a bucket

You can set the ACL of a bucket through the oss_create_bucket interface:

  1. aos_pool_t *p;
  2. oss_request_options_t *options;
  3. oss_acl_e oss_acl = OSS_ACL_PRIVATE;
  4. char *bucket_name = "<Name of your bucket>";
  5. aos_string_t bucket;
  6. aos_table_t *resp_headers;
  7. aos_status_t *s;
  8. aos_pool_create(&p, NULL);
  9. /* Create and initialize options */
  10. options = oss_request_options_create(p);
  11. init_options(options);
  12. /* Initialize parameters */
  13. aos_str_set(&bucket, bucket_name);
  14. /* Configure the access permissions of a bucket */
  15. s = oss_put_bucket_acl(options, &bucket, oss_acl, &resp_headers);
  16. if (aos_status_is_ok(s)) {
  17. printf("put bucket acl succeeded\n");
  18. } else {
  19. printf("put bucket acl failed\n");
  20. }
  21. aos_pool_destroy(p);

Notice:

  • Only the bucket owner has the permission to set the access permissions of the bucket.
  • The access permission of a bucket (oss_acl_e) is an enumeration value. The value options include OSS_ACL_PRIVATE, OSS_ACL_PUBLIC_READ, and OSS_ACL_PUBLIC_READ_WRITE.
Thank you! We've received your feedback.