Bucket Policy (Java SDK V2)
Manage bucket authorization policies by calling the PutBucketPolicy, GetBucketPolicy, and DeleteBucketPolicy operations with OSS Java SDK V2.
Prerequisites
OSS Java SDK V2 is installed. If you use Maven, add the following dependency to the pom.xml file:
com.aliyun:alibabacloud-oss-v2.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
By default, an Alibaba Cloud account has full permissions. However, the RAM users or RAM roles of the Alibaba Cloud account have no permissions. The Alibaba Cloud account or an account administrator must grant the required permissions to the RAM users or RAM roles.
API | Action | Description |
PutBucketPolicy |
| Configures, queries, or deletes a bucket policy. |
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.*;
import com.aliyun.sdk.service.oss2.transport.BinaryData;
public class BucketPolicySample {
public static void main(String[] args) throws Exception {
String region = "cn-hangzhou";
String bucket = "examplebucket";
CredentialsProvider provider = new EnvironmentVariableCredentialsProvider();
OSSClientBuilder clientBuilder = OSSClient.newBuilder()
.credentialsProvider(provider)
.region(region);
try (OSSClient client = clientBuilder.build()) {
// Set the bucket policy
String policy = "{\"Version\":\"1\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Action\":[\"oss:GetObject\"],\"Principal\":[\"*\"],"
+ "\"Resource\":[\"acs:oss:*:*:examplebucket/*\"]}]}";
PutBucketPolicyResult putResult = client.putBucketPolicy(PutBucketPolicyRequest.newBuilder()
.bucket(bucket)
.body(BinaryData.fromString(policy))
.build());
System.out.printf("PutBucketPolicy status code:%d%n", putResult.statusCode());
// Query the bucket policy
GetBucketPolicyResult getResult = client.getBucketPolicy(
GetBucketPolicyRequest.newBuilder().bucket(bucket).build());
System.out.printf("Bucket policy: %s%n", getResult.body());
// Delete the bucket policy
DeleteBucketPolicyResult delResult = client.deleteBucketPolicy(
DeleteBucketPolicyRequest.newBuilder().bucket(bucket).build());
System.out.printf("DeleteBucketPolicy status code:%d%n", delResult.statusCode());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For the sample code of synchronous calls, see PutBucketPolicy.java.
For the sample code of asynchronous calls, see PutBucketPolicyAsync.java.
For the sample code of synchronous calls, see GetBucketPolicy.java.
For the sample code of asynchronous calls, see GetBucketPolicyAsync.java.
For the sample code of synchronous calls, see DeleteBucketPolicy.java.
For the sample code of asynchronous calls, see DeleteBucketPolicyAsync.java.