Determine whether the bucket exists and whether there is permission to access it
Updated at:2025-11-03
Basic workflow
- Create an instance of the BOSClient class.
- Execute the BOSClient headBucket method;
- If no errors occur in the request, the bucket exists, and the requester has permission to access it.
Example code
Swift
1__block BOSHeadBucketResponse* response = nil;
2BCETask* task = [client headBucket:@"<bucketname>"];
3 task.then(^(BCEOutput* output) {
4 if (output.response) {
5 response = (BOSHeadBucketResponse*)output.response;
6 NSLog(@"head bucket success!");
7 }
8 if (output.error) {
9 NSLog(@"Bucket not exist or no permission!");
10 }
11});
12[task waitUtilFinished];
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4 // Initialize
5BCECredentials* credentials = [[BCECredentials alloc] init];
6credentials.accessKey = @"<access key>";
7credentials.secretKey = @"<secret key>";
8BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9configuration.credentials = credentials;
10BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11__block BOSHeadBucketResponse* response = nil;
12BCETask* task = [client headBucket:@"<bucketname>"];
13task.then(^(BCEOutput* output) {
14 if (output.response) {
15 response = (BOSHeadBucketResponse*)output.response;
16 NSLog(@"head bucket success!");
17 }
18 if (output.error) {
19 NSLog(@"Bucket not exist or no permission!");
20 }
21});
22[task waitUtilFinished];
23}
