Question

I am trying to upload images to Amazon S3, but they stay on Heroku only. What should I put in config.asset_host in the carrierwave.rb, when using the Carrierwave-aws gem?

This is my uploaders -> pictureuploader.rb

class PictureUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick

   include CarrierWave::MiniMagick
   version :thumb do
  process :resize_to_fill => [150, 150]
end

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

   def cache_dir
    "#{Rails.root}/tmp/uploads"
  end

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
   def default_url
       ActionController::Base.helpers.asset_path("fallback/#{version_name || :thumb}/default.png")
   end



  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
   def extension_white_list
     %w(jpg jpeg gif png)
   end
end

This is my config -> initializers -> carrierwave.rb

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = ENV['S3_BUCKET_NAME']
  config.aws_acl    = :public_read
  config.asset_host = ''
  config.aws_authenticated_url_expiration = 60 * 60 * 24 * 365


  config.aws_credentials = {
    access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
    secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
  }
end

I'm using the Figaro gem to store the Amazon S3 credentials and have the file in .gitignore. I have done >> rake figaro:heroku

My gemfile:

source 'https://rubygems.org'
ruby '2.0.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'


# Use sqlite3 as the database for Active Record
group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

gem 'carrierwave'

gem 'carrierwave-aws'

gem 'figaro'

gem 'aws-sdk'

gem 'mini_magick', '3.5.0'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

gem 'bootstrap-sass', '2.3.2.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

group :test, :development do
  gem "rspec-rails", "2.13.1"
end

group :test do
  gem "capybara", "2.1.0"
end

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]

In my event.rb model, I have added:

    require 'carrierwave/orm/activerecord'
mount_uploader :image_file_name, PictureUploader

And in my show.html.erb view I have:

<%= image_tag(@event.image_file_name_url, :width => 400) if @event.image_file_name.present? %>

I can upload images, but on my Amazon AWS console, it says that my bucket is empty. So currently they only stay on Heroku? What could I try next?

Was it helpful?

Solution

I managed to get this to work by changing the storage option to :aws and changing in production.rb the

config.action_controller.asset_host = "https://s3-us-west-1.amazonaws.com/mybucketname"

So now this question is solved, since the bucket is not empty any more.

However now I have a new issue, CSS doesn't load on Heroku, when I enabled an asset server (AWS) to serve images? Using Rails4, Carrierwave, Heroku, Amazon S3 since my solution broke the CSS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top