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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6524041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:37:36  来源:igfitidea点击:

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

javaapiamazon-s3amazon-web-services

提问by MikeNereson

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

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

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

具体来说,通过 Java AWS SDK 的哪些 API 方法可用于在上传对象时将其设为公开?

采纳答案by MikeNereson

Found the answer in an amazon aws forum.

亚马逊 aws 论坛中找到了答案。

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

The answer being

答案是

.withCannedAcl(CannedAccessControlList.PublicRead)

.withCannedAcl(CannedAccessControlList.PublicRead)

回答by Maurice

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

回答by Rajat

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

source

来源