Reputation: 3009
I am getting the following exception when trying to reproduce the sample code Amazon provides for uploading S3 objects to be server side encrypted using Amazon KMS (key management service):
com.amazonaws.AmazonClientException: please use region-specific endpoint to access buckets located in regions that require V4 signing.
::
Caused by: com.amazonaws.services.s3.model.AmazonS3Exception: Requests specifying Server Side Encryption with AWS KMS managed keys require AWS Signature Version 4. (Service: Amazon S3; Status Code: 400; Error Code: InvalidArgument;
The code used is:
public void uploadServerSideEncryptedFileToS3( String bucketName , String key , String sourceFilePath , String masterKey ) {
awsCredentials = new BasicAWSCredentials( awsAccessKey, awsSecretKey );
PutObjectRequest putObjectRequest = new PutObjectRequest( bucketName,
key , new File( sourceFilePath ) ).withSSEAwsKeyManagementParams( new SSEAwsKeyManagementParams( masterKey ) );
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setProtocol( Protocol.HTTPS );
AmazonS3 connection = new AmazonS3Client( awsCredentials , clientConfiguration );
connection.setRegion( com.amazonaws.regions.Region.getRegion( Regions.US_EAST_1 ) );
PutObjectResult response = connection.putObject( putObjectRequest );
}
Upvotes: 4
Views: 4032
Reputation: 7199
Here is the code I used for S3 upload
@Test
public void testNoMetaData() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonS3 amazonS3 = new AmazonS3Client(awsCredentials);
amazonS3.setRegion(Region.getRegion(region));
byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setSSEAlgorithm(SSEAlgorithm.KMS.getAlgorithm());
InputStream inputStream = new ByteArrayInputStream(bytes);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputStream, metadata);
putObjectRequest.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(awsKmsKeyId));
amazonS3.putObject(putObjectRequest);
}
Upvotes: 1