[英]How do you upload a zip file and unzip to s3?
我正在處理必須上傳zip文件的應用程序。 該zip文件基本上是一個靜態網站,因此它具有許多文件和幾個子目錄。 我已經使用rubyzip gem有一段時間了,無法弄清楚如何從其中提取文件。 關於一些示例,我可以從哪里獲得任何指導? 我確信以前有人遇到過這個問題。 rubyzip的文檔不是很好,所以我希望有人可以給我一些指導。
在這里,您可以找到一個我從未測試過的超級神奇的多線程zip至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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.