Search code examples
ruby-on-railsamazon-s3herokupaperclip

Paperclip S3 - Can upload images but cannot view them


I'm created a Rails app running on Heroku, with Paperclip and S3. I've managed to upload images to my S3 bucket through the site (I can see them show up in my bucket on the Amazon control panel).

But when I add an Image tag i.e. <%= image_tag x.photo.url %>, I get the following html (tags omitted here), with no image displayed!

img alt="Test_tree" src="http://s3.amazonaws.com/hiphotos/ads/photos/000/000/015/original/test_tree.jpg?1344661020"

Why can't I see the images even though they're in the bucket?


Solution

  • Firstly, the url you are trying to use up there in your code is this:

    http://s3.amazonaws.com/hiphotos/ads/photos/000/000/015/original/test_tree.jpg
    

    When you visit that link in the browser, you see the following:

    <message>
      The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
    </Message>
    <RequestId>810A6AE1D141304C</RequestId>
    <Bucket>hiphotos</Bucket>
    <HostId>
      XXZ+s+slgZLsRWy5NiU/G0yAKBLftw0oT2dDKpas532qXJEPSrISVPqfZsEgpb2J
    </HostId>
    <Endpoint>hiphotos.s3.amazonaws.com</Endpoint>
    

    So if we modify the url using the correct endpoint we get this:

    http://hiphotos.s3.amazonaws.com/ads/photos/000/000/015/original/test_tree.jpg
    

    Which does return the correct image.

    If you are using European buckets, this can happen, and it might be the fault of the gem you are using to push things to s3.

    There are loads of articles on how to get Paperclip, S3 and European buckets to play nicely together.

    I have found though, that since I started using the asset_sync gem, which uses Fog instead of aws-s3 gem, I don't have any more trouble with paperclip and S3.

    So I suspect Fog has something to do with making this problem go away for me. I'd recommend switching to it, if you're using something else.