2013-04-15 168 views
1

我正在研究一個我必須上傳一個zip文件的應用程序。該zip文件基本上是一個靜態網站,所以它有很多文件和一些子目錄。我一直在玩rubyzip寶石一段時間,並不知道如何簡單地從中提取文件。任何關於哪些地方我可以閱讀一些例子的指針?我相信以前有人遇到過這個問題。 rubyzip的文檔不是很好,所以我希望有人能給我一些指點。如何上傳zip文件並解壓縮到s3?

回答

0

在這裏,你去了一個超級神奇的多線程zip-to-S3上傳器,我還沒有測試 - 堅果!看起來我雖然已經三年太晚了。

class S3ZipUploader 

    require 'thread' 
    require 'thwait' 
    require 'find' 

    attr_reader *%i{ bucket s3 zip failed_uploads } 

    def initialize(zipfilepath, mys3creds) 
    # next 4 lines are important 
    @s3 = AWS::S3.new(access_key_id: mys3creds[Rails.env]['aws_access_key'], 
         secret_access_key: mys3creds[Rails.env]['aws_secret_access_key'], 
         region: 'us-west-2') 
    @bucket = @s3.buckets[ mys3creds[Rails.env]['bucket'] ] 

    @failed_uploads = [] 
    @zip = Zip::File.open(zipfilepath) 

    end 


    def upload_zip_contents 

    rootpath = "mypath/" 

    desired_threads = 10 
    total_entries = zip.entries.count 
    slice_size = (total_entries/desired_threats).ceil 
    threads = [] 
    zip.entries.each_slice(slice_size) do |e_arr| 
     threads << Thread.new do |et| 
     e_arr.each do |e| 
      result = upload_to_s3(rootpath + e.name, e.get_input_stream.read) 
      if !result 
      @failed_uploads << {name: e.name, entry: e, error: err} 
      end 
     end 
     end 
    end 
    ThreadsWait.all_waits(*threads) 
    end 

    def upload_file_to_s3(filedata,path, rewrite_basepath) 
    retries = 0 
    success = false 
    while !success && retries < 3 
     success = begin 
     obj = bucket.objects[path] 
     obj.write(Pathname.new(outputhtml)) 
     obj.acl = :public_read 
     success = true 
     rescue 
     retries += 1 
     success = false 
     end 
    end 
    return success 
    end 

end 

uploader = S3ZipUploader.new("/path/to/myzip.zip", MYS3CREDS) 
uploader.upload_zip_contents