Bucket permission control
Updated at:2025-11-03
Set bucket access permissions
Basic workflow
- Create an instance of the BOSClient class.
- Execute the BOSClient putBucketACL method;
- An error occurs if the settings cannot be applied.
Example code
Swift
1BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init];
2request.cannedAcl = BOS_ACL_PUBLIC_READ;
3request.bucket = @"<bucketname>";
4__block BOSPutBucketAclResponse* response = nil;
5BCETask* task = [client putBucketACL:request];
6task.then(^(BCEOutput* output) {
7 if (output.response) {
8 response = (BOSPutBucketAclResponse*)output.response;
9 NSLog(@"pub bucket acl success!");
10 }
11 if (output.error) {
12 NSLog(@"pub bucket acl failure with %@", output.error);
13 }
14});
15[task waitUtilFinished];
Note: The cannedAcl field can take 3 values:
Private,PublicReadandPublicReadWrite, which correspond to relevant permissions respectively. For specific contents, please refer to BOS API Documentation [Permission Control Using CannedAcl](BOS/API Reference/Access control.md#Permission control by CannedAcl).
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4 // Initialize
5BCECredentials* credentials = [[BCECredentials alloc] init];
6credentials.accessKey = @"<access key>";
7credentials.secretKey = @"<secret key>";
8BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9configuration.credentials = credentials;
10BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init];
12request.cannedAcl = BOS_ACL_PUBLIC_READ;
13request.bucket = @"<bucketname>";
14__block BOSPutBucketAclResponse* response = nil;
15BCETask* task = [client putBucketACL:request];
16task.then(^(BCEOutput* output) {
17 if (output.response) {
18 response = (BOSPutBucketAclResponse*)output.response;
19 NSLog(@"pub bucket acl success!");
20 }
21 if (output.error) {
22 NSLog(@"pub bucket acl failure with %@", output.error);
23 }
24});
25[task waitUtilFinished];
26}
Set access permissions for a specific user on the bucket
Basic workflow
- Instantiate the BOSClient class.
- To use the putBucketAcl method, you must create an instance of BOSPutBucketAclRequest to provide the authorized user information.
- An exception is thrown if the settings cannot be applied.
Example code
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4 // Initialize
5 BCECredentials* credentials = [[BCECredentials alloc] init];
6 credentials.accessKey = @"<access key>";
7 credentials.secretKey = @"<secret key>";
8 BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9 configuration.credentials = credentials;
10 BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 NSArray<NSString*>* grantee = @[
12 @"<grantee1>",
13 @"<grantee2>"
14 ];
15 NSArray<NSString*>* permission = @[
16 [BOSGrant permissionToString:BOSBucketGranteePermissionRead],
17 [BOSGrant permissionToString:BOSBucketGranteePermissionList],
18 ];
19 BOSGrant* grant = [[BOSGrant alloc] init];
20 grant.granteeIDArray = grantee;
21 grant.permission = permission;
22 BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init];
23 request.acl = [[BOSACL alloc] init];
24 request.acl.grantees = @[grant];
25 request.bucket = @"<bucketname>";
26 __block BOSPutBucketAclResponse* response = nil;
27 BCETask* task = [client putBucketACL:request];
28 task.then(^(BCEOutput* output) {
29 if (output.response) {
30 response = (BOSPutBucketAclResponse*)output.response;
31 NSLog(@"pub bucket acl success!");
32 }
33 if (output.error) {
34 NSLog(@"pub bucket acl failure with %@", output.error);
35 }
36 });
37 [task waitUtilFinished];
38}
Attention: The permission settings in Permission include five values:
READ,WRITE,LIST,GetObject, andFULL_CONTROL, which correspond to relevant permissions respectively. For specific contents, please refer to BOS API Documentation [Permission Control via Uploading ACL Files](BOS/API Reference/Access control.md#Permission control by uploading ACL files).
Complete example
Swift
1#import <BaiduBCEBasic/BaiduBCEBasic.h>
2#import <BaiduBCEBOS/BaiduBCEBOS.h>
3void example(void) {
4 // Initialize
5BCECredentials* credentials = [[BCECredentials alloc] init];
6credentials.accessKey = @"<access key>";
7credentials.secretKey = @"<secret key>";
8BOSClientConfiguration* configuration = [[BOSClientConfiguration alloc] init];
9configuration.credentials = credentials;
10BOSClient* client = [[BOSClient alloc] initWithConfiguration:configuration];
11 NSArray<NSString*>* grantee = @[
12 @"<grantee1>",
13 @"<grantee2>"
14];
15NSArray<NSString*>* permission = @[
16 [BOSGrant permissionToString:BOSBucketGranteePermissionRead],
17 [BOSGrant permissionToString:BOSBucketGranteePermissionList],
18];
19BOSGrant* grant = [[BOSGrant alloc] init];
20grant.granteeIDArray = grantee;
21grant.permission = permission;
22BOSPutBucketAclRequest* request = [[BOSPutBucketAclRequest alloc] init];
23request.acl = @[grant];
24request.bucket = @"<bucketname>";
25__block BOSPutBucketAclResponse* response = nil;
26BCETask* task = [client putBucketACL:request];
27task.then(^(BCEOutput* output) {
28 if (output.response) {
29 response = (BOSPutBucketAclResponse*)output.response;
30 NSLog(@"pub bucket acl success!");
31 }
32 if (output.error) {
33 NSLog(@"pub bucket acl failure with %@", output.error);
34 }
35});
36[task waitUtilFinished];
37}
