aws-sdk 2.3.0 和 Paperclip 5.0.0 坏区

aws-sdk 2.3.0 and Paperclip 5.0.0 bad region

我正在使用 AWS-SDK 2.3.0 gem 和 paperclip 5.0.0 gem。

在我的 config/environment/development.rb 文件中

config.paperclip_defaults = {
    storage: :s3,
    s3_region: 'eu-west-1',
    s3_credentials: {
      bucket: 'myBucketName',
      access_key_id: 'xxxxxxxxxxxxxxxxxxxxxx',
      secret_access_key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
      }
    }

创建存储桶时,我的存储桶区域设置为爱尔兰,因此根据 AWS 提供的文档,我将我的 s3 区域设置为 eu-west-1

我假设我的详细信息都是正确的,但是当我上传图片时,它会保存到存储桶中,但不会显示在我的 rails 应用程序中。如果我在新选项卡中右键单击打开图片,我会收到此错误:

<Message>
The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
</Message>

这看起来像是维护回形针的团队的已知问题。

https://github.com/thoughtbot/paperclip/issues/2151

aminariana commented on Mar 25 Workaround for now: Configure s3_host_name to s3-[region].amazonaws.com like so:

config.paperclip_defaults = { s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com", }

顺便说一句,只用谷歌搜索了确切的错误消息就找到了这个。

解决此问题的方法是创建一个名为:

的文件
config/initializers/paperclip.rb

并添加以下内容:

Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 's3-eu-west-1.amazonaws.com'

在第 3 行 eu-west-1 根据您所在的区域替换它。

我也遇到了这个问题。对我来说,问题是我在我的 ENV 中指定了错误的区域。确保通过单击存储桶的 "properties" 检查 S3 中的区域。我只是在亚马逊控制台的 URL 栏中查看区域,这可能就是我所在的区域。

另外,请注意美国标准区域 was renamed 到美国东部(弗吉尼亚北部),其区域字符串为 us-east-1.