Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 253
r123454321 Avatar asked Jan 01 '14 23:01

r123454321


People also ask

How many ways can you access an S3 bucket?

There are four access options – Public, Objects can be public, Only authorized users of this account, and Bucket and objects not public.


1 Answers

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

  :url =>':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename',
like image 127
r123454321 Avatar answered Oct 09 '22 05:10

r123454321