我试图将文件上传到s3存储桶,但出现“访问被拒绝”错误。因此,使用aws cli进行检查-发现我们需要传递SSE kms密钥ID才能使其正常工作
添加了以下内容的桶策略:s3:x-amz-server-side-encryption:aws:kms和s3:x-amz-server-side-encryption-aws-kms-key-id:密钥ID。下面是我的代码,该代码仍然会显示访问错误。
有人可以帮忙吗?
public class FileUploadToAWSS3 {
private static final String SUFFIX = "/";
private static final String kms_cmk_id = "arn:aws:kms:";
public void uploadFileToS3Bucket(MultipartFile multipartFile) throws IOException {
AWSCredentials credentials = new BasicAWSCredentials(
"access_key",
"secret_key");
AmazonS3 s3client = new AmazonS3Client(credentials);
s3client.setEndpoint("https://s3.eu-west-1.amazonaws.com");
String bucketName = "my-bucket";
s3client.createBucket(bucketName);
String quarter1 = "root/Folder1";
createFolder(bucketName, quarter1, s3client);
String fileName = quarter1 + SUFFIX + multipartFile.getOriginalFilename();
ObjectMetadata omd = new ObjectMetadata();
omd.setContentType(multipartFile.getContentType());
omd.setContentLength(multipartFile.getSize());
omd.setHeader("filename", multipartFile.getOriginalFilename());
s3client.putObject(new PutObjectRequest(bucketName, fileName,multipartFile.getInputStream(), omd)
.withCannedAcl(CannedAccessControlList.Private)
.withSSEAwsKeyManagementParams(new SSEAwsKeyManagementParams(kms_cmk_id)));
}
private void createFolder(String bucketName, String folderName, AmazonS3 client) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,
folderName + SUFFIX, emptyContent, metadata);
client.putObject(putObjectRequest);
}
}
答案 0 :(得分:0)
只需更新解决方法即可:-
由于使用了S3存储桶的服务实例(通过Cloud Foundry提供);因此创建了存储桶,并且不需要特定的create bucket命令。
删除了代码s3client.createBucket(bucketName);
,它可以正常工作。