问题描述
|
我有约16,000张图片要上传到Amazon。现在,它们在我的本地文件系统上。我想使用Paperclip将它们上传到S3,但是我不想首先将它们上传到我的服务器。我正在使用Heroku,并且它们会限制子弹的大小。
是否可以使用rake任务通过Paperclip将图像直接从本地文件系统上传到S3?
解决方法
您可以将应用程序配置为在开发中使用Amazon S3进行回形针存储(请参阅我的示例),并使用如下rake任务上传文件:
假设您的图片文件夹位于“ 0”中,则可以创建与此类似的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-与回形针/)。
, 很好的回答约翰尼·格拉斯和很好的问题克里斯。我在本地计算机Heroku,回形针和s3上有几百个tif文件。一些tiff文件的大小大于100MB,因此要让heroku注意长时间需要的延迟工作和一些额外的工作。由于这是一个主要的分批处理过程(从每个文件中创建了5种不同的图像形式,并上传了5次),因此耙任务的想法非常合适。如果有帮助,这里是我创建的rake任务,假设像Johnny那样写,您的开发数据库具有当前数据(使用pg备份获取新的ID集)并连接到S3。
我有一个名为\“ Item \”并带有附件\“ image \”的模型。我想检查现有商品是否已经有图片,如果没有,请上传新图片。效果是镜像源文件目录。不错的扩展可能是检查日期,看看是否更新了本地tif。
# 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