Rails 4 + Paperclip + S3: Changing endpoint to access an S3 bucket in User model?
Asked Answered
H

2

14

Should be a pretty simple fix to this question, I think, but I can't seem to get it to work. I have a Rails 4 app, a User model with a photograph attribute setup with Paperclip, and I have it linked to S3.

Here's the User model:

  has_attached_file :photograph,
  :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :storage => :s3,
  :bucket => " my-bucket-name ",
  :default_url => '/images/:attachment/missing_:style.png',
  :s3_credentials => S3_CREDENTIALS

The image gets added to my S3 bucket just fine, but when I try to render the picture with <%= image_tag @user.photograph.url %>, it doesn't show up. Upon further inspection, the image URL is:

http://s3.amazonaws.com/my-bucket-name/users/photographs/000/000/001/original/20121103_132556.jpg?1388619625

If I follow this URL in a browser, I see an XML file as follows:

<Error>
  <Code>PermanentRedirect</Code>
  <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> ... </RequestId>
  <Bucket>my-bucket-name</Bucket>
  <HostId>
    ...
  </HostId>
  <Endpoint>my-bucket-name.s3.amazonaws.com</Endpoint>
</Error>

Consequently, when I follow the url http://my-bucket-name.s3.amazonaws.com/actives/photographs/000/000/001/original/20121103_132556.jpg?1388619625 in a browser, I see the picture just fine.

How do I change the endpoint in my User model? What is the normal way to handle this? I must add that my S3 bucket is the Northern California region. Thanks.

Hutton answered 1/1, 2014 at 23:48 Comment(0)
H
24

Solved! Just added these two lines in my User model:

  :url =>':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename',
Hutton answered 2/1, 2014 at 0:26 Comment(0)
A
0

Sometimes the problem is that your bucket is not in the us server (amazon's default).

You can change the endpoint using this

:s3_host_name => 'your_host_name'

You can find your hostname in the following link under the endpoint column Amazon endpoints

If you don't know your bucket region, you can find it in bucket properties.

Awlwort answered 30/6, 2016 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.