无法将 spree 应用程序与 s3 连接

Unable to connect spree app with s3

我已经阅读了许多文档,但仍然存在于同一个问题上,我正在尝试将我的产品图片上传到本地 s3 和 heroku, 但不能这样做。在本地主机上,图像正在上传,但没有反映在 s3 存储桶控制台和 heroku 上,我遇到错误页面 请帮忙

config/initializers/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: "xxx",
        secret_access_key: "xxxxx",
        bucket: "xx",
      },


      storage:        :s3,
      s3_headers:     { "Cache-Control" => "max-age=31557600" },
      s3_protocol:    "https",
      bucket:         "xx",

      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'

ruby '2.2.2'
gem 'rails', '4.2.4'
group :development do
    gem 'sqlite3'
end 
group :production do
    gem 'pg'
    gem 'rails_12factor'
end
gem 'aws-sdk', '< 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'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end
gem 'spree', '3.0.4'
gem 'spree_gateway', github: 'spree/spree_gateway', branch: '3-0-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '3-0-stable'

我也试过注释掉

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

spree.rb 文件中根据之前在 Whosebug 上的 post 但这甚至没有帮助

您的配置文件中没有用于开发的 s3 配置

尝试通过在

中添加 else 部分来为开发环境添加类似的配置

S3 configuration

if Rails.env.production? then

...

end

(如果你知道,你可以按照其他方式) 也尝试发布日志,以便我们更好地帮助您。 顺便问一下,您是否已将 heroku 的环境设置为生产环境?

创建一个 config/s3.yml 文件并在其中添加 s3 配置。

development:
    AWS_ACCESS_KEY: xxx
    AWS_SECRET_TOKEN: xxx
    AWS_BUCKET: xx
production:
    AWS_ACCESS_KEY: ENV['AWS_ACCESS_KEY']
    AWS_SECRET_TOKEN: ENV['AWS_SECRET_TOKEN']
    AWS_BUCKET: ENV['AWS_BUCKET']
test:
    AWS_ACCESS_KEY: xxx
    AWS_SECRET_TOKEN: xxx
    AWS_BUCKET: ''

在config/application.rb中,在文件末尾添加。

\# This constant need to be loaded before it being used in config/initializers/paperclip.rb
S3_CREDENTIALS = YAML.load_file(File.join(Rails.root, 'config', 's3.yml'))[Rails.env]

然后你的 config/initializers/spree.rb 看起来像,

Spree.config do |config|
end
Spree.user_class = "Spree::User"
attachment_config = {
    s3_credentials: {
        access_key_id: S3_CREDENTIALS['AWS_ACCESS_KEY'],
        secret_access_key: S3_CREDENTIALS['AWS_SECRET_TOKEN'],
        bucket: S3_CREDENTIALS['AWS_BUCKET']
    },
    storage:        :s3,
    s3_protocol:    "http"
}
attachment_config.each do |key, value|
    Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end