我正在为我的Spring boot应用程序使用一个S3桶。
在这里,我创建了一个文件夹,并在S3 bucket中上传了文件,这些文件来自我的Spring boot应用程序,并得到了以下
upload
函数的帮助。现在,当我列出文件夹中的文件时,我能够看到它们。但我不能下载它们,总是得到403。
上传、列出对象和下载的代码片段。
//Download is failing
public File downloadObject(String filePath) {
File file = null;
log.info("Downloading object {} from s3 bucket {}", filePath, bucketName);
try {
file = File.createTempFile(filePath, "");
file.deleteOnExit();
amazonS3.getObject(new GetObjectRequest(bucketName, filePath), file);
} catch (Exception exception) {
exception.stackTrace();
return file;
//Following function is working perfectly fine
public List<String> listObjects(String pathPrefix) {
final ListObjectsV2Result listingResponse = amazonS3.listObjectsV2(new ListObjectsV2Request()
.withPrefix(pathPrefix)
.withBucketName(bucketName));
if (Objects.nonNull(listingResponse)) {
List<String> result = listingResponse.getObjectSummaries().stream().map(
S3ObjectSummary::getKey).collect(
Collectors.toList());
result.remove(pathPrefix);
return result;
return Collections.emptyList();
//uploading is also working fine
public void uploadFile(InputStream inputStream, String filePath)
try {
amazonS3.putObject(new PutObjectRequest(bucketName, filePath, inputStream, null));
} catch (SdkClientException exception) {
exception.stackTrace();
S3桶的权限如下。
"Version": "2012-10-17",
"Statement": [
"Sid": "AllowAReadWriteAccessToBucket",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456:role/abcd"
"Action": "s3:*",
"Resource": "arn:aws:s3:::test-bucket/*"
你可以看到,根据桶的策略,我已经给了所有的权限。即使这样,为什么下载还是失败了,我也搞不清楚。请帮助。