0

I'm trying to use Fog /Carrierwave/ with Rackspace Cloud Files. I have bunch of uploaded images in my production server. I'm trying to upload these images to Rackspace Cloud Files using below rake task.

desc 'Transfer photos to rackspace'
task :photos => :environment do
  photos = Photo.order(created_at: :desc).limit(10)
  photos.each do |photo|
    if photo.attachment?
      photo.attachment.recreate_versions!
      photo.save!
    else
      puts "================================= ATTACHMENT NOT FOUND: ID: #{photo.id}"
    end
  end
end

But I get following errors:

rake aborted!
undefined method `body' for nil:NilClass
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:227:in `read'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:77:in `sanitized_file'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:116:in `cache!'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/carrierwave-0.9.0/lib/carrierwave/uploader/versions.rb:225:in `recreate_versions!'
/home/zeck/code/bee/lib/tasks/bee.rake:9:in `block (4 levels) in <top (required)>'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each'
/home/zeck/code/bee/lib/tasks/bee.rake:7:in `block (3 levels) in <top (required)>'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/bin/ruby_noexec_wrapper:14:in `eval'
/home/zeck/.rvm/gems/ruby-2.0.0-p247@rails-4-1/bin/ruby_noexec_wrapper:14:in `<main>'

It means images not stored in Rackspace Cloud Files. You guys have similar rake task for it? Please share it to me. Or guide me.

Thank you for advice :D

Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
Zeck
  • 6,433
  • 21
  • 71
  • 111
  • I've got absolutely no experience with RackSpace, but a question I would ask is do you know why your task is trying to run the `body` method? – Richard Peck Dec 16 '13 at 09:22
  • Thank you for reply. Because it's calling file from RackSpace. That file is not existed in RackSpace. I'm not yet upload that file to RackSpace. This task should upload that file to RackSpace. – Zeck Dec 16 '13 at 09:23
  • Okay, and you should have the `body` method on the file object? – Richard Peck Dec 16 '13 at 09:23
  • Why? For ignore this error? – Zeck Dec 16 '13 at 09:24
  • Nooo, I meant - would you expect your app to be calling the `body` method on the file? If not, you may have problems in your `recreate_versions!` function? – Richard Peck Dec 16 '13 at 09:26
  • No I'm not calling `body` method on my uploader and rake task. – Zeck Dec 16 '13 at 09:30
  • So it's present when you try and load the image (from Rackspace?). I am asking because your error is this: `undefined method `body' for nil:NilClass`. Obviously, it's caused by the object being nil, but I'd love to know where / why you're calling the `body` method – Richard Peck Dec 16 '13 at 09:35
  • I'm not calling `body` method. Fog is calling it. Fog's trying to load the image from Rackspace which is not existed in Rackspace. Purpose of task's upload image to Rackspace. It's looks like I should use this [blog post](http://jondavidjohn.com/blog/2012/04/sync-rails-assets-with-rackspace-cloudfiles). BTW @RichPeck thank you for trying to help me :D – Zeck Dec 16 '13 at 09:46
  • 1
    :) Thanks for reply! As mentioned, have no experience with this particular issue, so will look at the blog post you recommended! Many thanks! – Richard Peck Dec 16 '13 at 09:47
  • Do you have a similar issue? If you found a solution let me know :D – Zeck Dec 16 '13 at 09:48
  • I'll let you know for sure - I've never had a problem with this stuff though :( – Richard Peck Dec 16 '13 at 10:48

1 Answers1

4

When you change the storage of a CarrierWave uploader from :file to :fog, it loses track of the original uploaded paths of the image files, so methods like recreate_versions! and store! won't be able to find the files to upload.

If you tell CarrierWave the old paths manually, it'll upload them to Cloud Files for you:

desc 'Transfer photos to rackspace'
task :photos => :environment do
  photos = Photo.order(created_at: :desc).limit(10)
  photos.each do |photo|
    if photo.attachment?
      # If you've overridden the storage path in the uploader, you'll need to
      # use a different path here.
      #
      # "photo[:attachment]" is used to get the actual attribute value instead
      # of the mounted uploader -- the base filename of the attachment file.
      path = Rails.root.join('public', 'uploads', photo[:attachment])

      unless path.exist?
        puts "#{path} doesn't exist. Double check your paths!"
        next
      end

      photo.attachment = path.open
      photo.save!
      puts "transferred #{photo.id}"
    else
      puts "================================= ATTACHMENT NOT FOUND: ID: #{photo.id}"
    end
  end
end
Ash Wilson
  • 22,820
  • 3
  • 34
  • 45