简体   繁体   English

carrierwave 在开发中上传到 s3,但不在生产中(heroku)

[英]carrierwave uploads to s3 in development but not in production (heroku)

I'm trying to make a simple upload to s3 working with carrierwave but somehow it isn't working in production on heroku: the files always get placed in the store_dir defined in the uploader but not in the s3 bucket.我正在尝试使用carrierwave对s3进行简单的上传,但不知何故它在heroku的生产中不起作用:文件总是放在上传store_dir定义的store_dir中,但不在s3存储桶中。

In development when I try it is working fine.在开发中,我尝试它工作正常。 I've followed the carrierwave github aws instructions but since it isn't working I'm out of ideas.我遵循了carrierwave github aws 说明,但由于它不起作用,我没有想法。

Here are my uploader and my carrierwave.rb files:这是我的上传器和我的carrierwave.rb 文件:

class PhotoUploader < CarrierWave::Uploader::Base
  storage :fog

  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end


 CarrierWave.configure do |config|
   config.fog_provider = 'fog/aws'                        # required
   config.fog_credentials = {
     provider:              'AWS',                        # required
     aws_access_key_id:     ENV['S3_KEY'],                        # required
     aws_secret_access_key: ENV['S3_SECRET'],                        # required
     region:                ENV['S3_REGION'],
     # host:                  's3.example.com',             # optional, defaults to nil
     # endpoint:              'https://s3.example.com:8080' # optional, defaults to nil
    }
   config.fog_directory  = ENV['S3_BUCKET']                                   # required
   config.fog_public     = false                                                 # optional, defaults to true
   config.fog_attributes = { cache_control: "public, max-age=#{365.days.to_i}" } # optional, defaults to {}
 end

Anyone has any ideas?任何人有任何想法?

Looking in the carrierwave Wiki on github, there is a section that explains how to make carrierwave work on heroku:查看github上的carrierwave Wiki,有一个部分解释了如何使carrierwave在heroku上工作:

You can work around this by setting the cache_dir in your Uploader classes to the tmp directory您可以通过将 Uploader 类中的 cache_dir 设置为 tmp 目录来解决此问题

class AvatarUploader < CarrierWave::Uploader::Base
 def cache_dir
   "#{Rails.root}/tmp/uploads"
 end
end

# config.ru
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # adding this line
run YourApplicationName::Application

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.root = Rails.root.join('tmp') # adding these...
  config.cache_dir = 'carrierwave' # ...two lines

  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => 'key',                        # required
    :aws_secret_access_key  => 'secret',                     # required
    :region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    :host                   => 's3.example.com',             # optional, defaults to nil
    :endpoint               => 'https://s3.example.com:8080' # optional, defaults to nil
  }
  config.fog_directory  = 'directory'                             # required
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}
end
CarrierWave.configure do |config|
  config.fog_credentials = {
    # In Heroku, follow http://devcenter.heroku.com/articles/config-vars
    #
    # $ heroku config:add S3_KEY=your_s3_access_key S3_SECRET=your_s3_secret S3_REGION=eu-west-1 S3_ASSET_URL=http://assets.example.com/ S3_BUCKET_NAME=s3_bucket/folder

    # Configuration for Amazon S3
    :provider              => 'AWS',
    :aws_access_key_id     => ENV['S3_KEY'],
    :aws_secret_access_key => ENV['S3_SECRET'],
    :region                => ENV['S3_REGION']
  }

  # For testing, upload files to local `tmp` folder.
  if Rails.env.test? || Rails.env.cucumber?
    config.storage = :file
    config.enable_processing = false
    config.root = "#{Rails.root}/tmp"
  else
    config.storage = :fog
  end

  config.cache_dir = "#{Rails.root}/tmp/uploads"                  # To let CarrierWave work on heroku

  config.fog_directory    = ENV['S3_BUCKET_NAME']
  config.s3_access_policy = :public_read                          # Generate http:// urls. Defaults to :authenticated_read (https://)
  config.fog_host         = "#{ENV['S3_ASSET_URL']}/#{ENV['S3_BUCKET_NAME']}"
end

Also, check out How to: Make Carrierwave work on Heroku and note the fact that making this work on Heroku by setting cache_dir to "#{Rails.root}/tmp/uploads" has the adverse side effect of making uploads not work across form redisplays.另外,请查看如何:使 Carrierwave 在 Heroku 上工作,并注意通过将 cache_dir 设置为"#{Rails.root}/tmp/uploads"来在 Heroku 上进行此工作具有使上传无法跨表单工作的不利副作用重新显示。

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

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