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 any of the following ways:
- 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:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an ObsClient instance. 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:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an ObsClient instance. 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:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an ObsClient instance. 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 Credentials page of OBS Console.
Obtaining an Object ACL
You can call ObsClient.getObjectAcl to obtain an object ACL. Sample code is as follows:
// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID. // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); String endPoint = "https://your-endpoint"; // Create an ObsClient instance. final ObsClient obsClient = new ObsClient(ak, sk, endPoint); AccessControlList acl = obsClient.getObjectAcl("bucketname", "objectname"); Log.i("GetObjectAcl", acl);
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot