36
votes

I have uploaded an image to Amazon S3 storage. But how can I access this image by url? I have made the folder and file public but still get AccessDenied error if try to access it by url https://s3.amazonaws.com/bucket/path/image.png

12

12 Answers

38
votes

This is an older question, but for anybody who comes across this question, once I made the file public I was able to access my image as https://mybucket.s3.amazonaws.com/myfolder/afile.jpg

12
votes

in my case i have uploaded image privately so that i was unable to access. i did following code

const AWS = require('aws-sdk')
const myBucket = 'BUCKET_NAME'
  const myKey = 'FILE_NAME.JPG'
  const signedUrlExpireSeconds = 60 * 1
  const s3 = new AWS.S3({
    accessKeyId: "ACCESS_KEY_ID",
    signatureVersion: 'v4',
    region: 'S3_REGION',
    secretAccessKey: "ACCESS_SECRET"
  });

  const url = s3.getSignedUrl('getObject', {
      Bucket: myBucket,
      Key: myKey,
      Expires: signedUrlExpireSeconds
  })

  console.log(url)
11
votes

You can access your image by using:

https://s3.amazonaws.com/bucketname/foldername/imagename.jpg

or if there are no folders, you can do:

https://s3.amazonaws.com/bucketname/imagename.jpg

upvote if helps. It conforms to present AWS dated 30 may 2017.

5
votes

Seems like you can now simply right-click on any folder inside a bucket and select 'Make Public' to make everything in that folder public. It may not work at the bucket level itself.

4
votes

make sure you access image using the same case as it was uploaded and stored on S3. for example, if you uploaded image_name.JPG, you should use the same name, but not image_name.jpg

3
votes

On your console, right click on the image you want to access and click on "Make Public"; when thats done right click on the image again and click on "Properties" and copy the Link from the Extended view.

3
votes

I came across this question whilst looking for a solution to a similar problem with being unable to access images.

It turns out that images with a % in their filename, when being accessed, must have the % symbol URL encoded to %25.

i.e. photo%20of%20a%20banana%20-%2019%20june%202016.jpg needs to be accessed via photo%2520of%2520a%2520banana%2520-%252019%2520june%25202016.jpg.

However, URL encoding the full path didn't work for us, since the slashes, etc would be encoded, and the path would not work. In our specific case, simply replacing % with %25 in all access paths made the difference.

2
votes

For future reference, if you want to access a file in Amazon S3 the URL needs to be something like:

bucketname.s3.region.amazonaws.com/foldername/image.png

Example: my-awesome-bucket.s3.eu-central-1.amazonaws.com/media/img/dog.png

Don't forget to set the object to public.

Inside S3 if you click on the object will you see a field called: Object URL. That's the object's web address.

1
votes

I was having the same problem. I have the issue the spacing in image url. I did this to make it work:

String imgUrl=prizes.get(position).getImagePreview().replaceAll("\\s","%20");

now pass this url to picasso:

Picasso.with(mContext)
            .load(imgUrl)
            .into(mImageView);
1
votes

One of easiest way is to make a bucket policy.

 {
       "Version": "2012-10-17",
       "Statement": [{
           "Sid": "MakeItPublic",
           "Effect": "Allow",
           "Principal": "*", 
           "Action": "s3:GetObject",
           "Resource": "arn:aws:s3:::yourbucketname.com/*"
    }]
   }
0
votes

To access private images via URL you must provide Query-string authentication. Query-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.

0
votes

Just addon to @akotian answer, you can get the object URL by clicking the object as follows get object url

and to access publically you can set the ACL programmatically while uploading the object to the bucket

i.e sample java request

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
            .contentType(contentType)
            .bucket(LOGO_BUCKET_NAME)
            .key(LOGO_FOLDER_PREFIX+fileName)
            .acl(ObjectCannedACL.PUBLIC_READ)// this make public read
            .metadata(metadata)
            .build();