[英]Amazon s3 file Public on upload
我需要調用withCannedAcl方法。 它將圖像作為公眾閱讀的ENUM進行轉換,因此,將圖書圖像注冊到存儲桶中時,它們將具有可讀的公共可見性,並且所有用戶都可以查看圖像。 如何將方法插入我的filesaver文件中?
這是我發現的withCannedAcl方法:
.withCannedAcl(CannedAccessControlList.PublicRead));
這是filesaver.java,我必須在其中插入該方法(可以在s3.putObject():?內部)。 我試過了但是沒用。
@RequestScoped
public class FileSaver {
private static final String CONTENT_DISPOSITION = "content-disposition";
private static final String FILENAME_KEY = "filename=";
@Inject
private AmazonS3Client s3;
public String write(String baseFolder, Part multipartFile) {
AmazonS3Client s3 = client();
String fileName = extractFilename(multipartFile.getHeader(CONTENT_DISPOSITION));
try {
s3.putObject("superkovalev", fileName,
multipartFile.getInputStream(),
new ObjectMetadata());
return "https://s3.amazonaws.com/xxxxxxxxxxxxxx/"
+fileName;
} catch (AmazonClientException | IOException e) {
throw new RuntimeException(e);
}
}
private AmazonS3Client client() {
AWSCredentials credentials = new BasicAWSCredentials("xxxxxxxx ",
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
AmazonS3Client newClient = new AmazonS3Client(credentials, new
ClientConfiguration());
newClient.setS3ClientOptions(new
S3ClientOptions().withPathStyleAccess(true));
return newClient;
}
private String extractFilename(String contentDisposition) {
if (contentDisposition == null) {
return null;
}
int startIndex = contentDisposition.indexOf(FILENAME_KEY);
if (startIndex == -1) {
return null;
}
String filename = contentDisposition.substring(startIndex
+ FILENAME_KEY.length());
if (filename.startsWith("\"")) {
int endIndex = filename.indexOf("\"", 1);
if (endIndex != -1) {
return filename.substring(1, endIndex);
}
} else {
int endIndex = filename.indexOf(";");
if (endIndex != -1) {
return filename.substring(0, endIndex);
}
}
return filename;
}
public AmazonS3Client getS3() {
return s3;
}
public void setS3(AmazonS3Client s3) {
this.s3 = s3;
}
}
這是問題的答案:
Policy bucket_policy = new Policy().withStatements(
new Statement(Statement.Effect.Allow)
.withPrincipals(Principal.AllUsers)
.withActions(S3Actions.GetObject)
.withResources(new Resource(
"arn:aws:s3:::" + bucket_name + "/*")));
https://docs.aws.amazon.com/pt_br/sdk-for-java/v1/developer-guide/examples-s3-bucket-policies.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.