这主要是一个句法问题,我似乎无法亲自动手。
这就是我现在所拥有的:
Zip::ZipFile.open(zipped_file, Zip::ZipFile::CREATE) do |zipfile|
zipfile.each do |file|
config = YAML.load_file(Rails.root + 'config/s3/s3.yml')[Rails.env]
AWS.config config # instantiate AWS creds..
# @filepath = .. I can't figure this out. How do I get a file and file path from a Zip::ZipEntry or ZipFile object to be able to upload a File object to S3..
AWS.s3.buckets[config['bucket']].objects.create("quizzes/" + v['id'] + "/" + file.to_s, file: @filepath )
建议,提示,想法?感谢..
答案 0 :(得分:1)
您发布的代码段实际上是创建一个zip文件而不是解压缩存档。 documentation有一个提取档案的例子。
对于文件,请查看ZipFsFile和目录ZipFsDIr。或者,您可以指定自己的目录,使其不在应用程序的目录中。
希望有所帮助!
答案 1 :(得分:1)
好的,这就是我所做的:
# zipped_file = the file in my params[:attachment]
file_list = Zip::ZipFile.open(zipped_file)
file_list.each do |file|
filename = file.name
basename = File.basename(filename)
tempfile = Tempfile.new(basename)
tempfile.binmode
tempfile.write file.get_input_stream.read
s3_obj = bucket.objects[ 'attachments/' + filename ]
s3_obj.write(tempfile)
end