Server-side encryption (Java SDK V2)
Manage server-side encryption rules for a bucket by calling the PutBucketEncryption, GetBucketEncryption, and DeleteBucketEncryption operations in OSS Java SDK V2.
Prerequisites
OSS Java SDK V2 is installed. If you use Maven, add the
com.aliyun:alibabacloud-oss-v2dependency topom.xml.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 under the account have no permissions by default. Grant the required permissions from the Alibaba Cloud account or an account administrator.
API | Action | Description |
PutBucketEncryption |
| Configure, query, or delete server-side encryption rules for 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 BucketEncryptionSample {
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()) {
// Configure server-side encryption (AES256)
ApplyServerSideEncryptionByDefault applyRule = ApplyServerSideEncryptionByDefault.newBuilder()
.sSEAlgorithm("AES256")
.build();
ServerSideEncryptionRule rule = ServerSideEncryptionRule.newBuilder()
.applyServerSideEncryptionByDefault(applyRule)
.build();
PutBucketEncryptionResult putResult = client.putBucketEncryption(
PutBucketEncryptionRequest.newBuilder()
.bucket(bucket)
.serverSideEncryptionRule(rule)
.build());
System.out.printf("PutBucketEncryption status code:%d%n", putResult.statusCode());
// Query the encryption configuration
GetBucketEncryptionResult getResult = client.getBucketEncryption(
GetBucketEncryptionRequest.newBuilder().bucket(bucket).build());
System.out.printf("SSE Algorithm: %s%n",
getResult.serverSideEncryptionRule().applyServerSideEncryptionByDefault().sSEAlgorithm());
// Delete the encryption configuration
DeleteBucketEncryptionResult delResult = client.deleteBucketEncryption(
DeleteBucketEncryptionRequest.newBuilder().bucket(bucket).build());
System.out.printf("DeleteBucketEncryption status code:%d%n", delResult.statusCode());
} catch (Exception e) {
System.out.printf("error:%n%s", e);
}
}
}
References
For the synchronous sample code, see PutBucketEncryption.java.
For the asynchronous sample code, see PutBucketEncryptionAsync.java.
For the synchronous sample code, see GetBucketEncryption.java.
For the asynchronous sample code, see GetBucketEncryptionAsync.java.
For the synchronous sample code, see DeleteBucketEncryption.java.
For the asynchronous sample code, see DeleteBucketEncryptionAsync.java.