我正在尝试将文件上传到S3存储桶。首先,我使用InstanceProfileCredentialsProvider创建一个AmazonS3客户端,然后从实例元数据中获取IAM凭证:
final AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new InstanceProfileCredentialsProvider(false))
.withRegion("eu-west-1").build();
与此实例相关联的角色具有访问文件并将文件上传到存储桶的策略:
{
"Version":"2012-10-17",
"Statement":{
"Action":[
"kms:Decrypt",
"kms:DescribeKey",
"kms:Encrypt",
"kms:GenerateDataKey",
"kms:ReEncryptFrom",
"kms:ReEncryptTo",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject*"
],
"Effect":"Allow",
"Resource":[
"arn:aws:kms:eu-west-1:[account-id]:key/[key-id]",
"arn:aws:s3:::[bucket-name]",
"arn:aws:s3:::[bucket-name]/[path-were-to-save-file]/*"
]}}
我正在尝试这样上传文件:
final PutObjectRequest request = new PutObjectRequest("[bucket-name]", file.getName(), file);
s3Client.putObject(request);
但是我得到拒绝访问AmazonS3ClientException。知道我缺少什么吗?
我还尝试将kms密钥包括在请求中:
final PutObjectRequest request = new PutObjectRequest("[bucket-name]", file.getName(), file)
.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams("[key-id]"));
但是它尝试从我的帐户中检索密钥:
arn:aws:kms:eu-west-1:[my-account-id]:key/[key-id]
不是策略中指定的帐户
arn:aws:kms:eu-west-1:[account-id]:key/[key-id]
并抛出KMSNotFoundException
答案 0 :(得分:0)
似乎我必须指定完整路径才能保存文件,并且可以放弃密钥:
EndOfLine
如果需要传递密钥,则不仅需要提供密钥ID,还需要提供完整的信息:
final PutObjectRequest request = new PutObjectRequest("[bucket-name]", "[path-were-to-save-file]/" + file.getName(), file);