Amazon S3 file upload not working - Public Read Permission

时间:2015-06-30 19:23:57

标签: java eclipse amazon-s3

I'm uploading a file to Amazon S3 using the eclipse SDK. Apparently the file uploads, actually you can see a file the same size of the original one (in this case a PDF) but with the same name as the key name. If you click on the file link, you get this:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<Error>
    <Code>AccessDenied</Code>
    <Message>Access Denied</Message>
    <RequestId>2FA4CE8A99D64D5B</RequestId>
    <HostId>
       t3k5TK/5PvqCNFGjtF5ycvpjS4HaTGcSTNrd8I8f4fe0JvFHdLMJnaO8N9MTZJe0fXm5BU6E+zU=
    </HostId>
</Error>

For this trial, I allowed all permissions to the bucket, why can't I see the PDF file?

EDIT

For downloading/viewing of the uploaded file, the Permission of the File while uploading, needs to be set as Public Read.

1 个答案:

答案 0 :(得分:2)

您可以使用withCannedAcl(CannedAccessControlList.PublicRead)将权限设置为公开阅读

public static void main(String[] args) throws IOException {
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {
        File file = new File(uploadFileName);
        s3client.putObject(new PutObjectRequest(
                                 bucketName, keyName, file).withCannedAcl(CannedAccessControlList.PublicRead)); // this will set the permission as PublicRead

     } catch (Exception ex) {
        ex.printStacktrace();
    } 
}