Manage object ACLs (Java SDK V2)
Use OSS Java SDK V2 to set or query the ACL of an object by specifying its version ID in a bucket with versioning enabled.
Prerequisites
OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency to the pom.xml file.Access credentials are configured by using environment variables. The sample code in this topic uses
EnvironmentVariableCredentialsProviderto read credentials from theOSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables.
Permissions
An Alibaba Cloud account has full permissions by default. RAM users and RAM roles under the account have no permissions by default and must be granted permissions by the Alibaba Cloud account or an account administrator.
API | Action | Description |
PutObjectAcl |
| Sets or queries the object ACL by version ID. |
Sample code
import com.aliyun.sdk.service.oss2.OSSClient;
import com.aliyun.sdk.service.oss2.OSSClientBuilder;
import com.aliyun.sdk.service.oss2.credentials.CredentialsProvider;
import com.aliyun.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider;
import com.aliyun.sdk.service.oss2.models.*;
public class VersioningObjectAclSample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
String bucket = "examplebucket";
String key = "exampleobject";
String versionId = "CAEQNhiBgM0BYiIDc4MGZj****";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
// Set the ACL of the specified object version
PutObjectAclResult putResult = client.putObjectAcl(PutObjectAclRequest.newBuilder()
.bucket(bucket)
.key(key)
.versionId(versionId)
.objectAcl("private")
.build());
System.out.printf("PutObjectAcl status code:%d, request id:%s%n",
putResult.statusCode(), putResult.requestId());
// Query the ACL of the specified object version
GetObjectAclResult getResult = client.getObjectAcl(GetObjectAclRequest.newBuilder()
.bucket(bucket)
.key(key)
.versionId(versionId)
.build());
System.out.printf("Object ACL: %s%n",
getResult.accessControlPolicy().accessControlList().grant());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For the synchronous sample code, see PutObjectAcl.java.
For the asynchronous sample code, see PutObjectAclAsync.java.
For the synchronous sample code, see GetObjectAcl.java.
For the asynchronous sample code, see GetObjectAclAsync.java.