判断Bucket是否存在,以及是否有权限访问
更新时间:2022-10-21
基本流程
- 创建BOSClient类的实例;
- 执行BOSClient headBucket方法;
- 当请求无错误发生时,说明Bucket存在且请求者有权限访问。
示例代码
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
9 if (output.error) {
10 NSLog(@"Bucket not exist or no permission!");
11 }
12});
13[task waitUtilFinished];
完整示例
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3
4void example(void) {
5// 初始化
6BCECredentials* credentials = [[BCECredentials alloc] init];
7credentials.accessKey = @"<access key>";
8credentials.secretKey = @"<secret key>";
9BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
10configuration.credentials = credentials;
11
12BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
13
14__block BOSHeadBucketResponse* response = nil;
15BCETask* task = [client headBucket:@"<bucketname>"];
16task.then(^(BCEOutput* output) {
17 if (output.response) {
18 response = (BOSHeadBucketResponse*)output.response;
19 NSLog(@"head bucket success!");
20 }
21
22 if (output.error) {
23 NSLog(@"Bucket not exist or no permission!");
24 }
25});
26[task waitUtilFinished];
27}