判断Bucket是否存在,以及是否有权限访问
更新时间:2022-10-21
基本流程
- 创建BOSClient类的实例;
- 执行BOSClient headBucket方法;
- 当请求无错误发生时,说明Bucket存在且请求者有权限访问。
示例代码
__block BOSHeadBucketResponse* response = nil;
BCETask* task = [client headBucket:@"<bucketname>"];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSHeadBucketResponse*)output.response;
NSLog(@"head bucket success!");
}
if (output.error) {
NSLog(@"Bucket not exist or no permission!");
}
});
[task waitUtilFinished];
完整示例
#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];
__block BOSHeadBucketResponse* response = nil;
BCETask* task = [client headBucket:@"<bucketname>"];
task.then(^(BCEOutput* output) {
if (output.response) {
response = (BOSHeadBucketResponse*)output.response;
NSLog(@"head bucket success!");
}
if (output.error) {
NSLog(@"Bucket not exist or no permission!");
}
});
[task waitUtilFinished];
}