查看Bucket列表
更新时间:2022-10-21
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient listBuckets方法,会返回BOSListBucketResponse类的实例。
- 对BOSListBucketResponse类型实例可以进行获取buckets/owner操作。
示例代码
如下代码可以列出用户所有的Bucket:
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
8 if (output.error) {
9 }
10});
11[task waitUtilFinished];
如下代码可以列出Bucket的Owner:
Swift
1BOSBucketOwner* owner = response.owner;
如下代码可以列出Bucket的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}
完整示例
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3
4void example(void) {
5 // 初始化
6 BCECredentials* credentials = [[BCECredentials alloc] init];
7 credentials.accessKey = @"<access key>";
8 credentials.secretKey = @"<secret key>";
9 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
10 configuration.credentials = credentials;
11
12 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
13
14 // 列举Buckets
15 __block BOSListBucketResponse* response = nil;
16 BCETask* task = [client listBuckets];
17 task.then(^(BCEOutput* output) {
18 if (output.response) {
19 response = (BOSListBucketResponse*)output.response;
20 }
21
22 if (output.error) {
23 }
24 });
25 [task waitUtilFinished];
26
27 // 获取Owner
28 BOSBucketOwner* owner = response.owner;
29 NSLog(@"the buckets owner is %@", owner.ownerID);
30
31 // 获取Bucket信息
32 NSArray<BOSBucketSummary*>* buckets = response.buckets;
33 for (BOSBucketSummary* bucket in buckets) {
34 NSLog(@"bucket name: %@", bucket.name);
35 NSLog(@"bucket location: %@", bucket.location);
36 NSLog(@"bucket create date: %@", bucket.createDate);
37 }
38}