View bucket list
Updated at:2025-11-03
Basic workflow
- Instantiate the BOSClient class.
- Using the BOSClient's listBuckets method returns an instance of the BOSListBucketResponse class.
- Instances of the BOSListBucketResponse class can retrieve bucket and owner information.
Example code
The following code can list all buckets of a user:
Swift
1__block BOSListBucketResponse* response = nil;
2BCETask* task = [client listBuckets];
3task.then(^(BCEOutput* output) {
4 if (output.response) {
5 response = (BOSListBucketResponse*)output.response;
6 }
7 if (output.error) {
8 }
9});
10[task waitUtilFinished];
The following code can list the bucket's owner:
Swift
1BOSBucketOwner* owner = response.owner;
The following code can list the mucket's metadata:
Swift
1NSArray<BOSBucketSummary*>* buckets = response.buckets;
2for (BOSBucketSummary* bucket in buckets) {
3 NSLog(@"bucket name: %@", bucket.name);
4 NSLog(@"bucket location: %@", bucket.location);
5 NSLog(@"bucket create date: %@", bucket.createDate);
6}
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4// Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11// List buckets
12 __block BOSListBucketResponse* response = nil;
13 BCETask* task = [client listBuckets];
14 task.then(^(BCEOutput* output) {
15 if (output.response) {
16 response = (BOSListBucketResponse*)output.response;
17 }
18 if (output.error) {
19 }
20 });
21 [task waitUtilFinished];
22// Get owner
23 BOSBucketOwner* owner = response.owner;
24 NSLog(@"the buckets owner is %@", owner.ownerID);
25// Get bucket information
26 NSArray<BOSBucketSummary*>* buckets = response.buckets;
27 for (BOSBucketSummary* bucket in buckets) {
28 NSLog(@"bucket name: %@", bucket.name);
29 NSLog(@"bucket location: %@", bucket.location);
30 NSLog(@"bucket create date: %@", bucket.createDate);
31 }
32}
