How do you make an S3 object public via the aws Java SDK?

后端 未结 3 1943
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 00:08

How do you make an S3 object public via the AWS Java SDK?

Specifically, what API methods via the Java AWS SDK can be used to make an Object public when its uploaded?

相关标签:
3条回答
  • 2020-11-30 00:50

    Found the answer in an amazon aws forum.

    return s3Client.putObject(
       new PutObjectRequest(bucketName, objectKey, inputStream, metadata)
          .withCannedAcl(CannedAccessControlList.PublicRead));
    

    The answer being

    .withCannedAcl(CannedAccessControlList.PublicRead)

    0 讨论(0)
  • 2020-11-30 00:56

    An alternative approach which allows for more finegrained control of who exactly is allowed to view the object (all users or authenticated users only):

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, image);
        AccessControlList acl = new AccessControlList();
        acl.grantPermission(GroupGrantee.AllUsers, Permission.Read); //all users or authenticated
        putObjectRequest.setAccessControlList(acl);
        s3client.putObject(putObjectRequest);
    
    0 讨论(0)
  • 2020-11-30 01:09
    s3client.setObjectAcl("bucketName[/subDirectory]", fileName, CannedAccessControlList.PublicRead);
    URL url = s3client.getUrl("bucketName[/subDirectory]", fileName);
    String sharableLink = url.toExternalForm();
    

    source

    0 讨论(0)
提交回复
热议问题