① Get multiple Zip files from S3
(2) Unzip multiple zip files into a directory format
③ I want to make one zip file in the end.
When I get_object in (1), it becomes as follows, and the necessary file path cannot be obtained in (2).
PK\u0003\u0004\u0014\u0000\u0000\u0000\u0000\u0000\xA1\x9AYQ\u0000\u0000\u0000
client = Aws::S3::Client.new(
:region => 'ap-northeast-1',
:access_key_id => '***',
:secret_access_key => '***',
)
@orders.each do |order|
obj = client.get_object(
bucket: "*",
key: order.zip_file_name
).body.read
Rails.logger.debug obj.inspect
#=> PK\u0003\u0004\u0014\u0000\u00....
end
The contents of the zip file are binary data.
Obj is referencing the contents of the downloaded file. The function .body.read
is a function that refers to the contents of the zip file. So, of course, the binary comes out in the log.
Open the zip file using rubyzip's open_buffer
method without reading.
However, the open_buffer method is not listed in the official rubyzip documentation. .. ..
I want to find a better way.
client = Aws::S3::Client.new(
:region => 'ap-northeast-1',
:access_key_id => '***',
:secret_access_key => '***',
)
@orders.each do |order|
obj = client.get_object(
bucket: "*",
key: order.zip_file_name
).body >>Don't read!
end
# get_open with object
Zip::File.open_buffer(obj) do |zip|
~~
end
Recommended Posts