Search code examples
amazon-s3codenameone

Codename one upload image to S3 bucket permission


Shai from codename one helped me to upload an image to s3 bucket and I am able to upload images and I happen to notice that when I access the image from browser I am getting Access denied error.

The image was loading fine on the mobile app, the same image throws Access denied error in the web browser.

This is my code:

   String strImage = Capture.capturePhoto(width, -1);
   String s3Bucket = "https://s3.amazonaws.com/MyBuckt/";
   String fileName = "Images/1234567";
   MultipartRequest rq = new MultipartRequest();
   rq.setUrl(s3Bucket);
   rq.addArgumentNoEncoding("key", fileName);
   rq.addArgument("acl", "public-read-write"); // here I give read-write access
   rq.addData("file", strImage, "image/jpeg");
   NetworkManager.getInstance().addToQueue();

When I launched S3 Browser and I do see the file and when I click on the file I get Access denied pop up message. The bucket and the folder has been given full grant permission as well as sub folders.

The image is captured by the phone camera.


Solution

  • After doing some research, I have found the solution for it. Hopefully it may help someone. Solution:

       String strImage = Capture.capturePhoto(width, -1);
       String s3Bucket = "https://s3.amazonaws.com/MyBuckt/";
       String fileName = "Images/1234567";
       MultipartRequest rq = new MultipartRequest();
       rq.setUrl(s3Bucket);
       rq.addArgumentNoEncoding("key", fileName);
       //rq.addArgument("acl", "public-read-write"); // here I give read-write access
       rq.addArgument("acl", "bucket-owner-full-control");
       rq.addData("file", strImage, "image/jpeg");
       NetworkManager.getInstance().addToQueue();
    

    I modified the acl access level as follows

    rq.addArgument("acl", "bucket-owner-full-control");
    

    What I understood from the readings, the uploaded item will not have any access except delete. With this acl access the file is now can be read.

    Link that I got the info from: https://stackoverflow.com/questions/34055084/s3-user-cannot-access-object-in-his-own-s3-bucket-if-created-by-another-user

    ** Thank you Shai for being very responsive and be helpful as much as you can **