简体   繁体   中英

Upload a file to AWS S3 with public access granted

I have a S3 bucket that is defined with public access, nevertheless, when I upload a file into it, and access to the URL I have an access denied error:

{
    Region region = Region.EU_WEST_1;
        S3Client s3 = S3Client.builder()
                .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
                .region(region)
                .build();

        String result = putS3Object(s3, "my-bucket", "pics/15/12345.jpg", "/opt/pics/15/12345.jpg");

        System.out.println(result);


    }

    // snippet-start:[s3.java2.s3_object_upload.main]
    public static String putS3Object(S3Client s3,
            String bucketName,
            String objectKey,
            String objectPath) {

        try {
            PutObjectResponse response = s3.putObject(PutObjectRequest.builder()
                            .bucket(bucketName)
                            .key(objectKey)
                            .build(),
                    RequestBody.fromBytes(getObjectFile(objectPath)));

            return response.eTag();

        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

    // Return a byte array
    private static byte[] getObjectFile(String filePath) {

        FileInputStream fileInputStream = null;
        byte[] bytesArray = null;

        try {
            File file = new File(filePath);
            bytesArray = new byte[(int) file.length()];
            fileInputStream = new FileInputStream(file);
            fileInputStream.read(bytesArray);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bytesArray;
    }

在此处输入图像描述

This is the info I see un the bucket:

Objects are the fundamental entities that are stored in Amazon S3. In order for other people to gain access to objects, you will have to explicitly grant them permissions.

I also tried with the V2 of the API

Your problem may have to do with the fact that although you configured public access to your bucket, probably by unchecking all the checkboxes in its Block public access configuration option, you still need to enable public access on the object itself when you create it by configuring the corresponding object ACL.

You can indicate the ACL for the object right in the put object operation. Please, consider the following modification in your code:

    // snippet-start:[s3.java2.s3_object_upload.main]
    public static String putS3Object(S3Client s3,
            String bucketName,
            String objectKey,
            String objectPath) {

        try {
            PutObjectResponse response = s3.putObject(
              PutObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                // this is the important modification, set a 
                // pre-established (canned) ACL of public-read for the object
                .acl(ObjectCannedACL.PUBLIC_READ)
                .build(),
                    RequestBody.fromBytes(getObjectFile(objectPath)));

            return response.eTag();

        } catch (S3Exception e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "";
    }

Please, see the relevant documentation of the Java API and object ACLs .

I see that you are using the V1 version of the S3 API. Try using V2. I have never seen an issue uploading an object to an Amazon S3 bucket that you own in your account.

Try this example:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/PutObject.java

If you have never used V2 before, i suggest you try this quick start: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM