将文件上传到 Amazon S3 时出现拒绝访问错误

Getting access denied error on uploading the file to Amazon S3

获取拒绝访问错误:

Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: AEB65D41FEB958C7; S3 Extended Request ID: jhCxO/Mv4MTxMRjvkM3bHVJ3dhb7TBeK0k7qHFUY3Q/RFhF591mRLeIzPXUEF0EdtgX9FNJXSq4=), S3 Extended Request ID: jhCxO/Mv4MTxMRjvkM3bHVJ3dhb7TBeK0k7qHFUY3Q/RFhF591mRLeIzPXUEF0EdtgX9FNJXSq4=File Upload Exception:Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: AEB65D41FEB958C7;

尝试上传文件时。我的代码是:

private void uploadFile(String fileName, File file) {
    System.out.print("clouldfileservice: putREqeust");
    PutObjectRequest request = new PutObjectRequest(this.bucketName, fileName, file)
            .withCannedAcl(CannedAccessControlList.PublicRead);
    try {
        PutObjectResult result = this.s3Client.putObject(request);
    } catch (Exception e) {
        System.out.print("File Upload Exception:" + e.toString());
        System.out.print("File Upload Exception:" + e.getLocalizedMessage());

    }

}

错误表明您无权访问 S3 存储桶。

正确解:

转到 policy generator 并生成一个,它是一个定义谁有权访问此资源的配置。

将该策略粘贴到 存储桶设置 > 权限 > 存储桶策略 并保存。

快速而肮脏的解决方案:

选中 "list" 和 "write" 复选框:存储桶设置 > 权限 > Public 访问 > 所有人.

转到:存储桶设置 > 权限 > 存储桶策略

正确的解决方案是从 PutObjectRequest 对象中删除 withCannedAcl(CannedAccessControlList.PublicRead);。您正在尝试设置可选的 pre-configured 访问控制策略以用于新对象,但您的存储桶具有私有访问权限。