java—将文件上载到aws s3并授予公共访问权限

ebdffaop  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(1186)

我有一个用public access定义的s3 bucket,然而,当我上传一个文件到其中,并且访问url时,我遇到了一个拒绝访问错误:

{
    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;
    }


这是我在桶里看到的信息:

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.

我还尝试了api的v2

vltsax25

vltsax251#

您的问题可能与这样一个事实有关,即尽管您配置了对bucket的公共访问,但可能是通过取消选中bucket中的所有复选框 Block public access 配置选项时,在通过配置相应的对象acl创建对象时,仍然需要对对象本身启用公共访问。
您可以在put object操作中正确指示对象的acl。请考虑对代码进行以下修改:

// 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 "";
    }

请参阅javaapi和对象acl的相关文档。

py49o6xq

py49o6xq2#

我看到您使用的是s3api的v1版本。尝试使用v2。我从来没有见过一个问题上传一个对象到一个amazons3桶,你在你的帐户自己。
请尝试以下示例:
https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javav2/example_code/s3/src/main/java/com/example/s3/putobject.java
如果您以前从未使用过v2,我建议您尝试以下快速入门:https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html

相关问题