无法使用s3连接spree应用程序

时间:2016-03-17 14:35:59

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

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

配置/初始化/ 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 文件中,根据上一篇关于stackoverflow的文章,但这甚至没有帮助

2 个答案:

答案 0 :(得分:0)

您在配置文件中没有s3配置进行开发

尝试在

中添加其他部分,为开发环境添加类似的配置
  

S3配置

     

如果Rails.env.production?然后

     

...

     

(如果你知道,你可以采取其他方式) 也尝试发布日志,以便我们可以更好地帮助您。 那么你有没有将heroku的环境设置为生产?

答案 1 :(得分:0)

创建一个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