Bucket管理
更新时间:2020-04-02
新建Bucket
-
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient putBucket方法,您需要提供Bucket的名字。
-
示例代码
Swift1BCETask* task = [client putBucket:@"<bucketname>"]; //新建一个Bucket,指定Bucket名称
-
完整示例
Swift1#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 14BCETask* task = [client putBucket:@"<bucketName>"]; 15task.then(^(BCEOutput* output) { // 任务可以异步执行。 16 if (output.response) { 17 // 任务执行成功。 18 } 19 20 if (output.error) { 21 // 任务执行失败。 22 } 23 24 if (output.progress) { 25 // 任务执行进度。 26 } 27}); 28[task waitUtilFinished]; // 可以同步方式,等待任务执行完毕。 29}
注意:由于Bucket的名称必须在所有区域中是唯一的,所以需要保证BucketName不与其他所有区域上的BucketName相同。
查看Bucket列表
-
基本流程
- 创建BOSClient类的实例。
- 执行BOSClient listBuckets方法,会返回BOSListBucketResponse类的实例。
- 对BOSListBucketResponse类型实例可以进行获取buckets/owner操作。
-
示例代码
如下代码可以列出用户所有的Bucket:
Swift1__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:
Swift1BOSBucketOwner* owner = response.owner;
如下代码可以列出Bucket的Metadata:
Swift1NSArray<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}
-
完整示例
Swift1#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}
判断Bucket是否存在,以及是否有权限访问
-
基本流程
- 创建BOSClient类的实例;
- 执行BOSClient headBucket方法;
- 当请求无错误发生时,说明Bucket存在且请求者有权限访问。
-
示例代码
Swift1__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];
-
完整示例
Swift1#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}
删除Bucket
-
基本流程
- 创建BOSClient类的实例;
- 执行BOSClient deleteBucket方法;
- 删除失败时会产生错误。
-
示例代码
Swift1__block BOSDeleteBucketResponse* response = nil; 2BCETask* task = [client deleteBucket:@"<bucketname>"]; 3task.then(^(BCEOutput* output) { 4 if (output.response) { 5 response = (BOSDeleteBucketResponse*)output.response; 6 NSLog(@"delete bucket success!"); 7 } 8 9 if (output.error) { 10 NSLog(@"delete bucket failure"); 11 } 12}); 13[task waitUtilFinished];
注意:如果Bucket不为空(即Bucket中有Object和未完成的三步上传Part存在),则Bucket无法被删除,必须清空Bucket后才能成功删除。
-
完整示例
Swift1#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 BOSDeleteBucketResponse* response = nil; 15BCETask* task = [client deleteBucket:@"<bucketname>"]; 16task.then(^(BCEOutput* output) { 17 if (output.response) { 18 response = (BOSDeleteBucketResponse*)output.response; 19 NSLog(@"delete bucket success!"); 20 } 21 22 if (output.error) { 23 NSLog(@"delete bucket failure"); 24 } 25}); 26[task waitUtilFinished]; 27}
Bucket权限控制
设置Bucket的访问权限
-
基本流程
- 创建BOSClient类的实例;
- 执行BOSClient putBucketACL方法;
- 设置失败时会产生错误。
-
示例代码
Swift1BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 2request.cannedAcl = BOS_ACL_PUBLIC_READ; 3request.bucket = @"<bucketname>"; 4 5__block BOSPutBucketAclResponse* response = nil; 6BCETask* task = [client putBucketACL:request]; 7task.then(^(BCEOutput* output) { 8 if (output.response) { 9 response = (BOSPutBucketAclResponse*)output.response; 10 NSLog(@"pub bucket acl success!"); 11 } 12 13 if (output.error) { 14 NSLog(@"pub bucket acl failure with %@", output.error); 15 } 16}); 17[task waitUtilFinished];
说明:cannedAcl 字段可取3个值:
Private
、PublicRead
和PublicReadWrite
,它们分别对应相关权限,具体内容可以参考《BOS API文档 使用CannedAcl方式的权限控制》。 -
完整示例
Swift1#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 14BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 15request.cannedAcl = BOS_ACL_PUBLIC_READ; 16request.bucket = @"<bucketname>"; 17 18__block BOSPutBucketAclResponse* response = nil; 19BCETask* task = [client putBucketACL:request]; 20task.then(^(BCEOutput* output) { 21 if (output.response) { 22 response = (BOSPutBucketAclResponse*)output.response; 23 NSLog(@"pub bucket acl success!"); 24 } 25 26 if (output.error) { 27 NSLog(@"pub bucket acl failure with %@", output.error); 28 } 29}); 30[task waitUtilFinished]; 31}
设置指定用户对Bucket的访问权限
-
基本流程
- 创建BOSClient类的实例。
- 执行putBucketACL方法,您需要创建一个BOSPutBucketAclRequest的实例来提供授权用户信息。
- 设置失败时会抛出异常。
-
示例代码
Swift1#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 NSArray<NSString*>* grantee = @[ 14 @"<grantee1>", 15 @"<grantee2>" 16 ]; 17 18 NSArray<NSString*>* permission = @[ 19 [BOSGrant permissionToString:BOSBucketGranteePermissionRead], 20 [BOSGrant permissionToString:BOSBucketGranteePermissionList], 21 ]; 22 23 BOSGrant* grant = [[BOSGrant alloc] init]; 24 grant.granteeIDArray = grantee; 25 grant.permission = permission; 26 BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 27 request.acl = [[BOSACL alloc] init]; 28 request.acl.grantees = @[grant]; 29 request.bucket = @"<bucketname>"; 30 31 __block BOSPutBucketAclResponse* response = nil; 32 BCETask* task = [client putBucketACL:request]; 33 task.then(^(BCEOutput* output) { 34 if (output.response) { 35 response = (BOSPutBucketAclResponse*)output.response; 36 NSLog(@"pub bucket acl success!"); 37 } 38 39 if (output.error) { 40 NSLog(@"pub bucket acl failure with %@", output.error); 41 } 42 }); 43 [task waitUtilFinished]; 44}
注意:Permission中的权限设置包含三个值:
READ
、WRITE
、LIST
、GetObject
和FULL_CONTROL
,它们分别对应相关权限,具体内容可以参考《BOS API文档 上传ACL文件方式的权限控制》。 -
完整示例
Swift1#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 NSArray<NSString*>* grantee = @[ 14 @"<grantee1>", 15 @"<grantee2>" 16]; 17 18NSArray<NSString*>* permission = @[ 19 [BOSGrant permissionToString:BOSBucketGranteePermissionRead], 20 [BOSGrant permissionToString:BOSBucketGranteePermissionList], 21]; 22 23BOSGrant* grant = [[BOSGrant alloc] init]; 24grant.granteeIDArray = grantee; 25grant.permission = permission; 26BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init]; 27request.acl = @[grant]; 28request.bucket = @"<bucketname>"; 29 30__block BOSPutBucketAclResponse* response = nil; 31BCETask* task = [client putBucketACL:request]; 32task.then(^(BCEOutput* output) { 33 if (output.response) { 34 response = (BOSPutBucketAclResponse*)output.response; 35 NSLog(@"pub bucket acl success!"); 36 } 37 38 if (output.error) { 39 NSLog(@"pub bucket acl failure with %@", output.error); 40 } 41}); 42[task waitUtilFinished]; 43}