简体   繁体   English

如何使用Spree设置Amazon S3?

[英]How to setup Amazon S3 with Spree?

I have tried reading a lot of things online, none of them helped. 我尝试过在线阅读很多东西,但没有一个帮助。 So need a concrete answer. 因此需要一个具体的答案。 How do I configure S3 to work with Spree? 如何配置S3与Spree配合使用? I am using Spree 3.0.1 with Rails 4.2.1. 我正在将Spree 3.0.1与Rails 4.2.1一起使用。

I have spree.rb as: 我有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

Also my Gemfile is 我的宝石文件也是

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'

First you need to add the gem 'aws-sdk' with version less than 2.0: 首先,您需要添加版本低于2.0的gem'aws-sdk':

gem 'aws-sdk', '< 2.0'

then, you need to add this code in the spree initializer: 然后,您需要在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

Can you try removing path from the settings: 您可以尝试从设置中删除path吗:

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

It seems odd to have a path that references your Rails root when the image will be stored in S3. 当映像将存储在S3中时,具有引用您的Rails根的path似乎很奇怪。

Use this gem 使用这个宝石

 gem 'aws-sdk', '< 2.0'

Use this snippet in config/initializers/spree.rb 在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 资源

First Remove your S3 configuration settings from your spree.rb Spree backend already have this configuration in General Settings => image settings => enable S3 . 首先,从spree.rb删除您的S3配置设置spree.rb后端在General Settings => image settings => enable S3已经具有此配置。

Hey you just need to enable the S3 from you spree backend. 嘿,您只需要从狂欢后端启用S3。 Provide the basic details required over there. 提供那里所需的基本详细信息。 Add a following line in you initilizer paperclip.rb 在您添加以下行initilizer 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

Your attachment Url should be :s3_eu_url as we initialized the interpolation for this URL in initilizer 您的附件网址应为:s3_eu_url因为我们在初始化程序中为此URL初始化了插值

I found my answer here: NameError (uninitialized constant Paperclip::Storage::S3::AWS): 我在这里找到了答案: NameError(未初始化的常量Paperclip :: Storage :: S3 :: AWS):

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

Apparantly the issue starts with a newer version of aws-sdk so you need a version that's lower than 2.0. 显然,问题始于aws-sdk的较新版本,因此您需要的版本低于2.0。

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

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