如何使用Spree设置Amazon S3?

时间:2015-07-10 08:42:56

标签: ruby-on-rails heroku amazon-s3 paperclip spree

我尝试过在线阅读很多东西,但没有一个能帮上忙。所以需要一个具体的答案。如何配置S3与Spree一起使用? 我正在使用Spree 3.0.1和Rails 4.2.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'

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

使用此宝石

 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

答案 3 :(得分:0)

首先从spree.rb Spree后端删除您的S3配置设置已在General Settings => image settings => enable S3中进行此配置。

嘿,你只需要从你的狂欢后端启用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

我们在initilizer中初始化此网址的插值时,您的附件网址应为:s3_eu_url

答案 4 :(得分:0)

我在这里找到了答案:NameError (uninitialized constant Paperclip::Storage::S3::AWS):

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

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