Block public access at the bucket level (Java SDK V2)
Manage the block public access configuration of a bucket by calling OSS API operations with OSS Java SDK V2.
Prerequisites
OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency to your 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 all permissions by default. RAM users and RAM roles that belong to an Alibaba Cloud account have no permissions by default. The Alibaba Cloud account or an account administrator must grant the required permissions before the RAM user or RAM role can perform the operations.
API | Action | Description |
PutBucketPublicAccessBlock |
| Sets, queries, or deletes the block public access configuration of a bucket. |
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 BucketPublicAccessBlockSample {
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()) {
// Enable block public access at the bucket level
BucketPublicAccessBlockConfiguration config = BucketPublicAccessBlockConfiguration.newBuilder()
.blockPublicAccess(true)
.build();
PutBucketPublicAccessBlockResult putResult = client.putBucketPublicAccessBlock(
PutBucketPublicAccessBlockRequest.newBuilder()
.bucket(bucket)
.bucketPublicAccessBlockConfiguration(config)
.build());
System.out.printf("PutBucketPublicAccessBlock status code:%d%n", putResult.statusCode());
// Query the block public access configuration of the bucket
GetBucketPublicAccessBlockResult getResult = client.getBucketPublicAccessBlock(
GetBucketPublicAccessBlockRequest.newBuilder().bucket(bucket).build());
System.out.printf("Block public access: %s%n",
getResult.publicAccessBlockConfiguration().blockPublicAccess());
// Delete the block public access configuration of the bucket
DeleteBucketPublicAccessBlockResult delResult = client.deleteBucketPublicAccessBlock(
DeleteBucketPublicAccessBlockRequest.newBuilder().bucket(bucket).build());
System.out.printf("DeleteBucketPublicAccessBlock status code:%d%n", delResult.statusCode());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For the synchronous sample code, see PutBucketPublicAccessBlock.java.
For the asynchronous sample code, see PutBucketPublicAccessBlockAsync.java.
For the synchronous sample code, see GetBucketPublicAccessBlock.java.
For the asynchronous sample code, see GetBucketPublicAccessBlockAsync.java.
For the synchronous sample code, see DeleteBucketPublicAccessBlock.java.
For the asynchronous sample code, see DeleteBucketPublicAccessBlockAsync.java.