Reputation: 377
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.
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.
Here are my uploader and my carrierwave.rb files:
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?
Upvotes: 0
Views: 735
Reputation: 2781
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.
Upvotes: 0
Reputation: 307
Looking in the carrierwave Wiki on github, there is a section that explains how to make carrierwave work on heroku:
You can work around this by setting the cache_dir in your Uploader classes to the tmp directory
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
Upvotes: 1