如何通过aws Java SDK公开S3对象?

时间:2011-06-29 16:29:28

标签: java api amazon-s3 amazon-web-services

如何通过AWS Java SDK公开S3对象?

具体而言,通过Java AWS SDK可以使用哪些API方法在上传时将对象公开?

3 个答案:

答案 0 :(得分:125)

amazon aws forum中找到答案。

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

答案是

.withCannedAcl(CannedAccessControlList.PublicRead)

答案 1 :(得分:2)

另一种方法可以更精确地控制确切地允许谁查看对象(仅限所有用户或经过身份验证的用户):

    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);

答案 2 :(得分:2)

s3client.setObjectAcl("bucketName[/subDirectory]", fileName, CannedAccessControlList.PublicRead);
URL url = s3client.getUrl("bucketName[/subDirectory]", fileName);
String sharableLink = url.toExternalForm();

source