簡體   English   中英

使用Paperclip將數千張圖像上傳到S3

[英]Uploading thousands of images with Paperclip to S3

我有約16,000張圖片要上傳到Amazon。 現在,它們在我的本地文件系統上。 我想使用Paperclip將它們上傳到S3,但是我不想首先將它們上傳到我的服務器。 我正在使用Heroku,它們會限制子彈的大小。

是否可以使用rake任務通過Paperclip將圖像直接從本地文件系統上傳到S3?

您可以將應用程序配置為在開發中使用Amazon S3進行回形針存儲( 請參閱我的示例 ),並使用如下rake任務上傳文件:

假設您的圖片文件夾位於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-回形針/ )。

約翰尼·格拉斯(Johnny Grass)很好的回答,克里斯(Chris)很好的問題。 我的本地計算機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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM