2013-12-16 41 views
0

我正在尝试使用Fog/Carrierwave/with Rackspace Cloud Files。我在生产服务器上有一堆上传的图像。我试图使用下面的rake任务将这些图像上传到Rackspace Cloud Files。如何使用rake任务将图像上传到Rackspace Cloud文件?

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 

,但我得到以下错误:

rake aborted! 
undefined method `body' for nil:NilClass 
/home/zeck/.rvm/gems/[email protected]/gems/carrierwave-0.9.0/lib/carrierwave/storage/fog.rb:227:in `read' 
/home/zeck/.rvm/gems/[email protected]/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:77:in `sanitized_file' 
/home/zeck/.rvm/gems/[email protected]/gems/carrierwave-0.9.0/lib/carrierwave/uploader/cache.rb:116:in `cache!' 
/home/zeck/.rvm/gems/[email protected]/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/[email protected]/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:13:in `each' 
/home/zeck/.rvm/gems/[email protected]/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/[email protected]/bin/ruby_noexec_wrapper:14:in `eval' 
/home/zeck/.rvm/gems/[email protected]/bin/ruby_noexec_wrapper:14:in `<main>' 

这意味着没有存储在Rackspace公司云文件的图像。你们有类似的佣金任务吗?请分享给我。或者引导我。

谢谢你的忠告:d

+0

我得到了与Rackspace公司完全没有经验,但一个问题,我要问的你知道为什么你的任务试图运行'body'方法? –

+0

谢谢你的回复。因为它是从RackSpace调用文件。该文件在RackSpace中不存在。我还没有将该文件上传到RackSpace。该任务应该将该文件上传到RackSpace。 – Zeck

+0

好的,你应该在文件对象上使用'body'方法? –

回答

4

当您从:file改变CarrierWave上传的storage:fog,它失去的轨道图像文件的原始上传的路径,所以像recreate_versions!store!方法不会能够找到要上传的文件。

如果手动古道告诉CarrierWave,它就会将它们上传到云文件为您提供:

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 
+0

非常感谢你:D – Zeck

相关问题