我有大约16,000张图片需要上传到亚马逊。目前,它们在我的本地文件系统中。我想使用Paperclip将它们上传到S3,但我不想先将它们上传到我的服务器。我正在使用Heroku,他们限制了Slug大小。
有没有办法使用rake任务通过Paperclip直接从我的本地文件系统将图像上传到S3?
我有大约16,000张图片需要上传到亚马逊。目前,它们在我的本地文件系统中。我想使用Paperclip将它们上传到S3,但我不想先将它们上传到我的服务器。我正在使用Heroku,他们限制了Slug大小。
有没有办法使用rake任务通过Paperclip直接从我的本地文件系统将图像上传到S3?
your_app_folder/public/images
,您可以创建一个类似于这样的rake任务。namespace :images do
desc "Upload images."
task :create => :environment do
@images = Dir["#{RAILS_ROOT}/public/images/*.*"]
for image in @images
MyModel.create(:image => File.open(image))
end
end
end
是的。在我的第一个个人Rails项目上,我做了类似的事情。这里有一个以前的SO问题(Paperclip S3下载远程图像),其答案链接到我很久以前找到答案的地方(http://trevorturk.com/2008/12/11/easy-upload-via-url-with-paperclip/)。
# lib/image_management.rake
namespace :images do
desc 'upload images through paperclip with postprocessing'
task :create => :environment do
directory = "/Volumes/data/historicus/_projects/deeplandscapes/library/tifs/*.tif"
images = Dir[directory]
puts "\n\nProcessing #{ images.length } images in #{directory}..."
items_with_errors = []
items_updated = []
items_skipped = []
images.each do |image|
# find the needed record
image_basename = File.basename(image)
id = image_basename.gsub("it_", "").gsub(".tif", "").to_i
if id > 0
item = Item.find(id) rescue nil
# check if it has an image already
if item
unless item.image.exists?
# create the image
success = item.update_attributes(:image => File.open(image))
if success
items_updated << item
print ' u '
else
items_with_errors << item
print ' e '
end
else
items_skipped << item
print ' s '
end
else
print "[#{id}] "
end
else
print " [no id for #{image_basename}] "
end
end
unless items_with_errors.empty?
puts "\n\nThe following items had errors: "
items_with_errors.each do |error_image|
puts "#{error_image.id}: #{error_image.errors.full_messages}"
end
end
puts "\n\nUpdated #{items_updated.length} items."
puts "Skipped #{items_skipped.length} items."
puts "Update complete.\n"
end
end