java - 使用 Amazon KMS 上传带有服务器端加密的 S3 对象时出错

标签 java encryption amazon-web-services amazon-s3

我在尝试重现 Amazon 提供的示例代码时遇到以下异常,用于上传 S3 对象以使用 Amazon KMS( key 管理服务)进行服务器端加密:

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;

使用的代码是:
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 );
}

最佳答案

这是我用于 S3 上传的代码

    @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);
}

关于java - 使用 Amazon KMS 上传带有服务器端加密的 S3 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29285454/

相关文章:

java - 使用 Android 异步 HTTP 时没有得到响应(来自 loopj)

java - 使用黑莓使用 AES/CBC/PKCS5Padding 解密数据

iphone - ShareKit加密应用提交

amazon-web-services - 上载Flask应用程序时,AWS Elastic Beanstalk错误

java - java awt 中的顶级窗口是什么?

java - HTTP 状态 405 - 此 URL 不支持 HTTP 方法 POST

Java - 从 PHP 网页 URL 读取一个 txt 文件

c# - 无法解密 RSA 加密 key

amazon-web-services - 使用 Amazon Elasticsearch Service 的 elastic4s

ruby - Amazon S3 在 Ruby SDK 中通过 URL 获取 S3 对象