Amazon s3 file Public on upload











up vote
0
down vote

favorite












I need to call the withCannedAcl method. It turns images as a public read ENUM, so when the book images are registered in the Bucket, they will have a readable public visibility and all users will be able to view the images. How can I insert the method into my filesaver file?



Here the withCannedAcl method that i found:



.withCannedAcl(CannedAccessControlList.PublicRead));


this is the filesaver.java where i have to insert that method (could be inside the s3.putObject():?). I tried but it didnt works.



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

}









share|improve this question
























  • Possible duplicate of How do you make an S3 object public via the aws Java SDK?
    – Wim Deblauwe
    Nov 22 at 17:10










  • Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
    – Sergio Guerjik
    Nov 22 at 17:25










  • s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
    – Sergio Guerjik
    Nov 22 at 17:50

















up vote
0
down vote

favorite












I need to call the withCannedAcl method. It turns images as a public read ENUM, so when the book images are registered in the Bucket, they will have a readable public visibility and all users will be able to view the images. How can I insert the method into my filesaver file?



Here the withCannedAcl method that i found:



.withCannedAcl(CannedAccessControlList.PublicRead));


this is the filesaver.java where i have to insert that method (could be inside the s3.putObject():?). I tried but it didnt works.



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

}









share|improve this question
























  • Possible duplicate of How do you make an S3 object public via the aws Java SDK?
    – Wim Deblauwe
    Nov 22 at 17:10










  • Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
    – Sergio Guerjik
    Nov 22 at 17:25










  • s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
    – Sergio Guerjik
    Nov 22 at 17:50















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to call the withCannedAcl method. It turns images as a public read ENUM, so when the book images are registered in the Bucket, they will have a readable public visibility and all users will be able to view the images. How can I insert the method into my filesaver file?



Here the withCannedAcl method that i found:



.withCannedAcl(CannedAccessControlList.PublicRead));


this is the filesaver.java where i have to insert that method (could be inside the s3.putObject():?). I tried but it didnt works.



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

}









share|improve this question















I need to call the withCannedAcl method. It turns images as a public read ENUM, so when the book images are registered in the Bucket, they will have a readable public visibility and all users will be able to view the images. How can I insert the method into my filesaver file?



Here the withCannedAcl method that i found:



.withCannedAcl(CannedAccessControlList.PublicRead));


this is the filesaver.java where i have to insert that method (could be inside the s3.putObject():?). I tried but it didnt works.



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

}






java amazon-s3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:56









Krease

11.2k74059




11.2k74059










asked Nov 22 at 17:01









Sergio Guerjik

359




359












  • Possible duplicate of How do you make an S3 object public via the aws Java SDK?
    – Wim Deblauwe
    Nov 22 at 17:10










  • Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
    – Sergio Guerjik
    Nov 22 at 17:25










  • s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
    – Sergio Guerjik
    Nov 22 at 17:50




















  • Possible duplicate of How do you make an S3 object public via the aws Java SDK?
    – Wim Deblauwe
    Nov 22 at 17:10










  • Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
    – Sergio Guerjik
    Nov 22 at 17:25










  • s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
    – Sergio Guerjik
    Nov 22 at 17:50


















Possible duplicate of How do you make an S3 object public via the aws Java SDK?
– Wim Deblauwe
Nov 22 at 17:10




Possible duplicate of How do you make an S3 object public via the aws Java SDK?
– Wim Deblauwe
Nov 22 at 17:10












Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
– Sergio Guerjik
Nov 22 at 17:25




Thank you Wim I tried again but i cant fix my filesaver.java could you pls give me a tip how to introduce that method in the filesaver.java ?
– Sergio Guerjik
Nov 22 at 17:25












s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
– Sergio Guerjik
Nov 22 at 17:50






s3.putObject("bucket", fileName, multipartFile.getInputStream(), new ObjectMetadata()).withCannedAcl(CannedAccessControlList.PublicRead); return "s3.amazonaws.com/superkovalev" +fileName; **doesnt work
– Sergio Guerjik
Nov 22 at 17:50














1 Answer
1






active

oldest

votes

















up vote
0
down vote













This is the answer for the question:



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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435521%2famazon-s3-file-public-on-upload%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This is the answer for the question:



    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






    share|improve this answer

























      up vote
      0
      down vote













      This is the answer for the question:



      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






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This is the answer for the question:



        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






        share|improve this answer












        This is the answer for the question:



        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 23:58









        Sergio Guerjik

        359




        359






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435521%2famazon-s3-file-public-on-upload%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo