We recently migrated from AWS SDK 1.x to 2.x for Java and put operation for AWS S3 is not happening. S3 bucket is encryption enabled ( using AWS KMS key).
Below is the code I am trying to use but I am getting error mentioned below that
Server Side Encryption with AWS KMS managed key requires HTTP header x-amz-server-side-encryption : aws:kms (Service: S3, Status Code: 400, Request ID: xxx, Extended Request ID: xxx/rY9ydIzxi3NROPiM=)
Update : I figured it out myself. So anyone who wants to benefit to connect to AWS S3 bucket using KMS key and AWS SDK 2.x for Java use the below code it should work
Map<String, String> metadata = new HashMap<>();
metadata.put("x-amz-server-side-encryption", "aws:kms");
PutObjectRequest request = PutObjectRequest.builder()
//.bucketKeyEnabled(true)
.bucket(bucketName)
.key(Key)
.metadata(metadata)
.serverSideEncryption(ServerSideEncryption.AWS_KMS)
.ssekmsKeyId("arn:aws:kms:xxxxx")
.build();
File outputFile = new File("filename");
try (PrintWriter pw = new PrintWriter(outputFile)) {
data.stream()
.forEach(pw::println);
}
awsS3Client.putObject(request, RequestBody.fromBytes(String.join(System.lineSeparator(),
data).getBytes(StandardCharsets.UTF_8)));