0
votes

I am unable to upload images using CarrierWave and S3. I am using these gems:

gem 'carrierwave'
gem 'carrierwave-aws'

I have the configuration set up properly, in the code, environment variables, and on AWS:

CarrierWave.configure do |config|
  config.storage    = :aws
  config.aws_bucket = ENV["S3_BUCKET_NAME"]
  config.aws_acl    = 'public-read'
  config.aws_credentials = {
    access_key_id:     ENV["AWS_ACCESS_KEY_ID"],
    secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
    region:            ENV["AWS_REGION"]
  }
  config.cache_dir = "#{Rails.root}/tmp/uploads"
end

class ImageUploader < CarrierWave::Uploader::Base
  storage :aws

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

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

One source said to include the cache_dir lines specifically for Heroku inside the uploader, but it seems to have no effect:

https://github.com/carrierwaveuploader/carrierwave/wiki/how-to%3A-make-carrierwave-work-on-heroku

Here is some standard code to save an image, but it gives an error:

> url = "https://dl.dropboxusercontent.com/u/22125572/Kira_opt.jpg"
> image = open(url)
> user = User.first
> user.image = image
TypeError: no implicit conversion of nil into String
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:171:in `join'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:171:in `cache_path'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:143:in `block in cache!'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/uploader/cache.rb:134:in `cache!'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/mount.rb:329:in `cache'
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/mount.rb:163:in `image='
from /app/vendor/bundle/ruby/2.2.0/gems/carrierwave-0.11.2/lib/carrierwave/orm/activerecord.rb:39:in `image='
from (irb):4
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
1
Did you set your ENV keys?siegy22
Yes, I copy pasted from AWS with heroku config:set S3_BUCKET_NAME=, and the same for the 3 othersEric

1 Answers

0
votes

It looks like the way I should have saved the remote image is using:

url = "https://dl.dropboxusercontent.com/u/22125572/Kira_opt.jpg"
user = User.first
user.remote_image_url = url
user.save

which means the url doesn't need to be opened at all.