51

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

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266

18 Answers18

45

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

akotian
  • 3,885
  • 1
  • 33
  • 44
19

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)
Renish Gotecha
  • 2,232
  • 22
  • 21
  • 1
    Thank you for this answer! This was the easiest way of solving my problem without making everything public! – alexcodes Dec 06 '20 at 19:53
  • Im trying to find some good documentation to learn how to upload an image to S3, the aws official documentation feels very cryptic, any tips? This answer alone is a very useful example to get a URL, but im looking for good examples to upload an image from my node app – xunux Feb 26 '21 at 03:46
  • 1
    great answer, exactly what I'm looking for. – angry kiwi Jul 02 '23 at 18:22
12

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.

Zohaib Ejaz
  • 387
  • 4
  • 18
  • 3
    I thin' this no longer works: `the bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint` – pmiranda Jan 20 '21 at 19:20
9

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/*"
    }]
   }
Alankar More
  • 1,107
  • 2
  • 8
  • 26
6

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.

Raja Rao
  • 5,691
  • 2
  • 28
  • 31
  • This is great if you're trying to set all existing files to public. As you upload files, they apparently don't get this "public" status. If adding the object via an API, you can set that "public" access at the time of upload - http://stackoverflow.com/questions/10055016/how-to-mark-a-certain-s3-file-as-make-public – Mattiavelli Sep 23 '16 at 21:48
5

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.

Joao Antunes
  • 193
  • 1
  • 12
  • Hi Jaoa. I've downvoted this answer because I don't think it adds any new information - this question is almost a decade old and there are already several answers (including the accepted answer) that are similar to this one. Please review and consider deleting this answer if appropriate. – Captain Hat Jan 23 '21 at 01:58
  • Hello @CaptainHat! At least for me, the accepted answer didn't work. And the reason was that the link is different. This could be because S3 updated its link URL. I thought to post a comment, in case someone had the same issue. – Joao Antunes Jan 23 '21 at 14:12
  • Oh okay. Thanks for clarifying. Consider posting this content as a comment on the relevant answer? I'll leave it to your judgement. – Captain Hat Jan 23 '21 at 14:13
  • Unfortunately, I only have 41 reputation points. Stackoverflow only allows you to comment on other people's answers when you have 50+ :( – Joao Antunes Jan 23 '21 at 14:20
4

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

Erikas Pliauksta
  • 1,402
  • 1
  • 14
  • 22
3

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.

Derekdunes
  • 64
  • 5
3

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.

Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
2

Just add Permission to follow the below image.

Justa add Permission

BC TUBE
  • 664
  • 5
  • 15
1

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);
Omar Faroque Anik
  • 2,531
  • 1
  • 29
  • 42
0

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.

Bill Zelenko
  • 2,606
  • 1
  • 17
  • 26
0

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();
Amol Patil
  • 347
  • 3
  • 11
0
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicRead",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": [
                "arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"
            ]
        }
    ]
}

use this policy for that bucket, which makes it public.

0

Adding bucket policy worked for me

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::yourbucketname/*"
        }
    ]
}
Sher Sanginov
  • 431
  • 5
  • 9
0

Turn off Block public access (bucket settings) from Permissions tab inside your bucket. You also need to Edit the permissions of the object. Provide Read access on Grantee Everyone (public access). Then chech "I understand the effects of these changes on this object." and Save changes.

washiur17
  • 617
  • 1
  • 6
  • 12
0

Try to access by :

https://s3.amazonaws.com//file.format

or go to the file and click "open" so it will redirect you the right path

-3

Change your Bucket.. It may works