查看Bucket列表
更新时间:2022-10-21
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient listBuckets方法,会返回BOSListBucketResponse类的实例。
- 对BOSListBucketResponse类型实例可以进行获取buckets/owner操作。
示例代码
如下代码可以列出用户所有的Bucket:
__block BOSListBucketResponse* response = nil;
BCETask* task = [client listBuckets];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSListBucketResponse*)output.response;
}
if (output.error) {
}
});
[task waitUtilFinished];
如下代码可以列出Bucket的Owner:
BOSBucketOwner* owner = response.owner;
如下代码可以列出Bucket的Metadata:
NSArray<BOSBucketSummary*>* buckets = response.buckets;
for (BOSBucketSummary* bucket in buckets) {
NSLog(@"bucket name: %@", bucket.name);
NSLog(@"bucket location: %@", bucket.location);
NSLog(@"bucket create date: %@", bucket.createDate);
}
完整示例
#import <BaiduBCEBasic/BaiduBCEBasic.h>
#import <BaiduBCEBOS/BaiduBCEBOS.h>
void example(void) {
// 初始化
BCECredentials* credentials = [[BCECredentials alloc] init];
credentials.accessKey = @"<access key>";
credentials.secretKey = @"<secret key>";
BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
configuration.credentials = credentials;
BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
// 列举Buckets
__block BOSListBucketResponse* response = nil;
BCETask* task = [client listBuckets];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSListBucketResponse*)output.response;
}
if (output.error) {
}
});
[task waitUtilFinished];
// 获取Owner
BOSBucketOwner* owner = response.owner;
NSLog(@"the buckets owner is %@", owner.ownerID);
// 获取Bucket信息
NSArray<BOSBucketSummary*>* buckets = response.buckets;
for (BOSBucketSummary* bucket in buckets) {
NSLog(@"bucket name: %@", bucket.name);
NSLog(@"bucket location: %@", bucket.location);
NSLog(@"bucket create date: %@", bucket.createDate);
}
}