Managing Object ACLs
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the API Reference
Object ACLs, similar to bucket ACLs, support pre-defined access control policies and direct configuration. For details, see Managing Bucket ACLs.
An object ACL can be configured in three modes:
- Specify a pre-defined access control policy during object upload.
- Call ObsClient.setObjectAcl to specify a pre-defined access control policy.
- Call ObsClient.setObjectAcl to set the ACL directly.
Specifying a Pre-defined Access Control Policy During Object Upload
Sample code:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
PutObjectRequest request = new PutObjectRequest();
request.setBucketName("bucketname");
request.setObjectKey("objectname");
request.setFile(new File("localfile")); // localfile indicates the path of the local file to be uploaded. You need to specify the file name.
// Set the object ACL to public-read.
request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
obsClient.putObject(request);
Setting a Pre-defined Access Control Policy for an Object
Sample code:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// Set the object ACL to private.
obsClient.setObjectAcl("bucketname", "objectname", AccessControlList.REST_CANNED_PRIVATE);
Directly Setting an Object ACL
Sample code:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
AccessControlList acl = new AccessControlList();
Owner owner = new Owner();
owner.setId("ownerid");
acl.setOwner(owner);
// Grant the FULL_CONTROL permission to a specified user.
acl.grantPermission(new CanonicalGrantee("userid"), Permission.PERMISSION_FULL_CONTROL);
// Grant the READ permission to all users.
acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
obsClient.setObjectAcl("bucketname", "objectname", acl);
The owner or grantee ID needed in the ACL indicates the account ID, which can be viewed on the My Credential page of OBS Console.
Obtaining an Object ACL
You can call ObsClient.getObjectAcl to obtain an object ACL. Sample code is as follows:
String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);
AccessControlList acl = obsClient.getObjectAcl("bucketname", "objectname");
Log.i("GetObjectAcl", acl);
Last Article: Obtaining Object Properties
Next Article: Listing Objects
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.