class AvatarImageUploader < CarrierWave::Uploader::Base | |
include CarrierWave::MiniMagick | |
def store_dir | |
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | |
end | |
version :print do | |
version :thumb { process :resize_to_fit => [32, 32] } | |
version :preview { process :resize_to_fit => [256, 256] } | |
version :full { process :resize_to_fit => [2048, 2048] } | |
end | |
version :web do | |
version :thumb { process :resize_to_fit => [32, 32] } | |
version :preview { process :resize_to_fit => [128, 128] } | |
version :full { process :resize_to_fit => [1024, 768] } | |
end | |
# ... | |
end |
# config/initializers/carrierwave.rb | |
CarrierWave.configure do |config| | |
config.fog_credentials = { | |
# Configuration for Amazon S3 should be made available through an Environment variable. | |
# For local installations, export the env variable through the shell OR | |
# if using Passenger, set an Apache environment variable. | |
# | |
# 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 |
<!-- Nested Versions --> | |
<%= image_tag avatar.avatar_image.url(:web, :preview) %> | |
<%= image_tag avatar.avatar_image.url(:print, :full) %> | |
<%= image_tag avatar.avatar_image.url(:web, :thumb) %> |
This comment has been minimized.
This comment has been minimized.
This is great! I removed config.s3_access_policy and config.fog_host and it's working very well with fog 1.3.1. |
This comment has been minimized.
This comment has been minimized.
Thanks. I needed to rename the initializer to config/initializers/fog.rb to get it to work. |
This comment has been minimized.
This comment has been minimized.
What is the S3_ASSET_URL? |
This comment has been minimized.
This comment has been minimized.
S3_ASSET_URL is the address of the server where you're uploading your files. For example, if you have the next URL for a uploaded file (that you can get from your S3 console at file properties) https://s3-eu-west-1.amazonaws.com/my_bucket_name/my_awesome_image.png S3_ASSET_URL="https://s3-eu-west-1.amazonaws.com" |
This comment has been minimized.
This comment has been minimized.
config.s3_access_policy= is no longer available. To force http, you can use config.fog_use_ssl_for_aws = false |
This comment has been minimized.
This comment has been minimized.
config.fog_host= doesn't seems to be working. |
This comment has been minimized.
This comment has been minimized.
This gist totally saved me when I was stuck, thank you!!!!! |
This comment has been minimized.
This comment has been minimized.
awesome. It works great. Just don't forget to initialise fog.rb! |
This comment has been minimized.
This comment has been minimized.
@grammakov |
This comment has been minimized.
This comment has been minimized.
The working configuration for me was # config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => ENV['S3_KEY'],
:aws_secret_access_key => ENV['S3_SECRET']
}
config.fog_directory = ENV['S3_BUCKET_NAME']
config.cache_dir = "#{Rails.root}/tmp/uploads" # For Heroku
end
|
This comment has been minimized.
This comment has been minimized.
And to cleanup your after your specs, you can add this. The wiki example say # spec/support/carrierwave.rb
RSpec.configure do |config|
# config.after(:all, :fog) do
config.after(:all) do
FileUtils.rm_rf CarrierWave::Uploader::Base.cache_dir
end
end |
This comment has been minimized.
This comment has been minimized.
One crucial component...where is the code that goes into the view to facilitate upload? Locally, you can add <% f.file_field :image %> and this creates an upload button that allows you to upload an image. What is the equivalent of that here? |
This comment has been minimized.
This comment has been minimized.
@jackson-sandland I believe it is no different than the local approach:
Also, check out How to: Make Carrierwave work on Heroku and note the fact that making this work on Heroku by setting Also, don't use |
This comment has been minimized.
This comment has been minimized.
Is this MiniMagick specific? What would be the ImageMagick way of doing this?
|
This comment has been minimized.
This comment has been minimized.
@abatko do you how to get the functionality of |
This comment has been minimized.
This comment has been minimized.
an update would be nice since this documentation is over 7 years old. unfortunately i was not able to set up carrierwave with s3 correctly |
This comment has been minimized.
This works great!