【问题标题】:Rails paperclip doesn't display uploaded images from Amazon S3Rails 回形针不显示从 Amazon S3 上传的图像
【发布时间】:2015-09-18 19:47:33
【问题描述】:

我将图像上传到 Amazon S3,但它们没有显示。

我收到此错误:

<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>
<Bucket>cb123</Bucket>
<Endpoint>cb123.s3.amazonaws.com</Endpoint>
<RequestId>870BC2E8570EF4E7</RequestId>
<HostId>
yuBkeXxftr7O9Ib0SasFTq8Hlvgc7hkhx9VMr+VwRL74qSDgJ9rqMgEU9noRIQe/
</HostId>
</Error>

这是我的设置:

has_attached_file :image, styles: { medium: "400x400#", small: "250x250#", :url =>':s3_domain_url', :path => '/:class/:attachment/:id_partition/:style/:filename' }

谢谢!

【问题讨论】:

标签: ruby-on-rails amazon-web-services heroku amazon-s3


【解决方案1】:

将此添加到您的 application.rb 或每个环境的配置文件中:

config.paperclip_defaults = {
  :storage => :s3,
  :s3_host_name => 's3-eu-central-1.amazonaws.com',
  :s3_credentials => {
    :bucket => 'your bucket',
    :access_key_id => 'your access-key-id',
    :secret_access_key => 'your secret-access-key'
  }, 
  :url =>':s3_domain_url',
  :path => '/:class/:attachment/:id_partition/:style/:filename'
}

然后,您可以从模型中删除 :url:path 配置。

【讨论】:

  • 我将此添加到“application.rb”并从我的模型中删除,但仍然存在相同的错误。也许问题是我将存储桶的区域设置为法兰克福?
  • 似乎回形针在美国以外的水桶上有问题,但这些设置应该可以解决它。您是否重新启动服务器以确保新配置生效?
  • 另外,您使用的是什么版本的 aws gem?是gem 'aws-sdk', '&lt; 2.0'
  • 尝试添加这个(将更新答案)::s3_host_name =&gt; 's3-eu-central-1.amazonaws.com' 确保在配置完成后重复上传过程
【解决方案2】:

在此处添加我的代码,因为自上次回答以来语法略有变化。

您需要将 S3_host_name 添加到 config/environments/development.rb 和 production.db 中的 config.paperclip_defaults 中。

这里是完整的代码 sn-p,将 us-west-2 更改为您的存储桶所在的任何区域:

config.paperclip_defaults = {
  storage: :s3,
  s3_host_name: 's3-us-west-2.amazonaws.com',
  s3_credentials: {
    bucket: ENV.fetch('S3_BUCKET_NAME'),
    access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
    secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
    s3_region: ENV.fetch('AWS_REGION'),
  }
}

【讨论】:

    【解决方案3】:

    尝试将其添加到您的模型中,您的 has_attached_file 方法存在:

    :url =>':s3_domain_url'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-13
      • 2014-11-14
      • 1970-01-01
      • 2012-12-20
      • 2016-01-04
      • 2015-01-20
      • 1970-01-01
      • 2019-07-15
      相关资源
      最近更新 更多