如何使用 Spree 设置 Amazon S3?

How to setup Amazon S3 with Spree?

我尝试在网上阅读很多东西,none 其中有帮助。所以需要一个具体的答案。如何配置 S3 以与 Spree 一起使用? 我正在使用带有 Rails 4.2.1.

的 Spree 3.0.1

我有 spree.rb 作为:

Spree.config do |config|
   # Example:
   # Uncomment to override the default site name.
  #  config.site_name = "Gazella Running Costumes"
  #  config.logo = "store/rungazella.png"

  #S3 configuration
  if Rails.env.production? then
       #production. Store images on S3.
       # development will default to local storage
      attachment_config = {
      s3_credentials: {
        access_key_id: ENV["S3_KEY"],
        secret_access_key: ENV["S3_SECRET"],
        bucket: ENV["S3_BUCKET"],
      },


      storage:        :s3,
      s3_headers:     { "Cache-Control" => "max-age=31557600" },
      s3_protocol:    "https",
      bucket:         ENV["S3_BUCKET"],

      path:          ":rails_root/public/:class/:attachment/:id/:style/:basename.:extension",
      default_url:   "/:class/:attachment/:id/:style/:basename.:extension",
      default_style: "product",
      }

      attachment_config.each do |key, value|
           Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
      end
  end
Spree.user_class = "Spree::User"
end

我的 Gemfile 也是

source 'https://rubygems.org'
gem 'rails', '4.2.1'
ruby '2.2.0'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
    gem 'byebug'
    gem 'web-console', '~> 2.0'
    gem 'spring'
end
gem 'spree', '3.0.1'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'
gem 'rails_12factor', group: :production
gem 'pg'
gem 'aws-sdk'

您可以尝试从设置中删除 path 吗:

path: ":rails_root/public/:class/:attachment/:id/:style/:basename.:extension",

当图像将存储在 S3 中时,有一个 path 引用您的 Rails 根似乎很奇怪。

首先你需要添加gem 'aws-sdk' 版本小于2.0:

gem 'aws-sdk', '< 2.0'

然后,您需要在 spree 初始化程序中添加此代码:

Rails.env.production?
  attachment_config = {
    s3_credentials: {
      access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      bucket:            ENV['S3_BUCKET_NAME']
    },

    storage:        :s3,
    s3_headers:     { "Cache-Control" => "max-age=31557600" },
    s3_protocol:    "https",
    bucket:         ENV['S3_BUCKET_NAME'],
    url:            ":s3_domain_url",

    path:           "/:class/:id/:style/:basename.:extension",
    default_url:    "/:class/:id/:style/:basename.:extension",
    default_style:  "product"
  }

  attachment_config.each do |key, value|
    Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
  end
end

使用这个 gem

 gem 'aws-sdk', '< 2.0'

在 config/initializers/spree.rb

中使用此代码段
spree.config do |config|
  attachment_config={
  s3_credentials: {
  access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
  bucket: ENV['ENV_BUCKET_NAME']
  },
  storage:     :s3,
  s3_headers:  {"Cache-Control"=>"max-age=31557600"},
  s3_protocol: "https",
  bucket:      ENV['S3_BUCKET_NAME'],
  url:         "url",
  styles:      {
                 mini:   "48x48>",
                 small:  "100x100>",
                 product:"240x240>",
                 large:  "600x600>"
               },
  path:        "/spree/:class/:id/:style/:basename.:extension",
  default_url: "/spree/products/:id/:style/:basename.:extension",
  default_style: "product",
  }
  attachment_config.each do |key,value|

   Spree::Image.attachment_definitions[:attachment][key.to_sym] = value

  end

end

source

首先从 spree.rb Spree 后端删除 S3 配置设置 General Settings => image settings => enable S3

嘿,您只需要从您的 spree 后端启用 S3。在那里提供所需的基本详细信息。 在启动器中添加以下行 paperclip.rb

if Rails.env.production?
    Paperclip.interpolates(:s3_eu_url) do |attachment, style|
      "#{attachment.s3_protocol}://#{Spree::Config[:s3_host_alias]}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
    end
    Paperclip::Attachment.default_options[:use_timestamp] = false
    Paperclip::Attachment.default_options[:s3_protocol] = "https"
end

您的附件 Url 应该是 :s3_eu_url 因为我们在 initilizer

中为此 URL 初始化了插值

我在这里找到了答案:

将您的 gem 文件 aws-sdk gem 更改为: gem aws-sdk, < 2.0

显然问题始于 aws-sdk 的较新版本,因此您需要低于 2.0 的版本。