简体   繁体   English

Rails 4 + Paperclip + S3:更改端点以访问用户模型中的S3存储桶?

[英]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. 我有一个Rails 4应用程序,一个用Paperclip设置photograph属性的用户模型,我把它链接到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. 图像很好地添加到我的S3存储桶中,但是当我尝试使用<%= image_tag @user.photograph.url %>渲染图片时,它不会显示。 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: 如果我在浏览器中关注此URL,我会看到一个XML文件,如下所示:

<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. 因此,当我在浏览器中关注网址http://my-bucket-name.s3.amazonaws.com/actives/photographs/000/000/001/original/20121103_132556.jpg?1388619625时,我看到图片就好了。

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. 我必须补充一点,我的S3斗是北加州地区。 Thanks. 谢谢。

Solved! 解决了! Just added these two lines in my User model: 刚在我的User模型中添加了这两行:

  :url =>':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename',

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 您可以在端点列Amazon端点下的以下链接中找到主机名

If you don't know your bucket region, you can find it in bucket properties. 如果您不知道您的存储区域,可以在存储桶属性中找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM