Check If Bucket Exists
Updated at:2025-11-03
Check if bucket exists
If users need to determine whether a bucket exists, the following code can achieve this:
Plain Text
1void test_head_bucket(bos_request_options_t *options, bos_string_t bucket, bos_table_t *resp_headers) {
2 bos_status_t *status = NULL;
3 status = bos_head_bucket(options, &bucket, &resp_headers);
4 print_headers(resp_headers);
5 if (bos_status_is_ok(status)) {
6 printf("head bucket succeeded\n");
7 } else {
8 printf("head bucket failed\n");
9 }
10 bos_bucket_exist_status_e bucket_exist;
11 status = bos_check_bucket_exist(options, &bucket, &bucket_exist, &resp_headers);
12 if (bucket_exist == BOS_BUCKET_NON_EXIST) {
13 printf("bucket: %.*s non exist.\n", bucket.len, bucket.data);
14 } else if (bucket_exist == BOS_BUCKET_EXIST) {
15 printf("bucket: %.*s exist.\n", bucket.len, bucket.data);
16 } else {
17 printf("bucket: %.*s unknown status.\n", bucket.len, bucket.data);
18 log_status(status);
19 }
20}
Note: If the bucket is not null (that is, there are objects in the bucket), the bucket cannot be deleted. The bucket must be emptied before it can be deleted successfully.
