[RUBY] What to do when a null byte error occurs

Thing you want to do

① 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.

The problem that occurred

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

Cause

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.

solution

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

What to do when a null byte error occurs
What to do when a javax.el.PropertyNotWritableException occurs
Error ExecJS :: RuntimeUnavailable: What to do when it occurs
What to do when javax.batch.operations.JobStartException occurs
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
What to do when IllegalStateException occurs in PlayFramework
What to do if a SAX Parser error occurs when using Liferay 7 / DXP on AWS
[Rails 6] What to do when a missing a template error occurs after introducing haml [Super easy]
What to do and how to install when an error occurs in DXRuby 1.4.7
What to do if an error occurs when nokogiri enters when bundle install
Notes on what to do when a WebView ClassNotFoundException occurs in JavaFX 12
What to do if ClassNotFoundException occurs when starting Tomcat
What to do when rails creates a 〇〇 2.rb file
What to do if an ActionController :: UnknownFormat error occurs
[Ubuntu] What to do when MongoDB cannot be started due to a SocketException error
What to do if mysql2 gets a bundle install error
What to do if you get a port error when docker-compose up on Mac
What to do if you get a gcc error in Docker
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
What to do if you get a DISPLAY error in gym.render ()
What to do when you become a Problem During Content Assist
A reminder when an aapt.exe error occurs
What to do when undefined method ʻuser_signed_in?'
What to do when you run into a docker-compose node_modules problem
What to do if you get a JNI shared library error when trying to build in Eclipse
What to do if an error occurs when doing npm install axios in React + Typescript project
no space left on device What to do if an error occurs
What to do if you get an error when you hit Heroku logs
What to do when "call'Hoge.connection' to establish a connection" appears on rails c
[Ruby] When adding a null constraint to a table
What to do when debugging "Source not found"
What do you use when converting to String?
What to do if you get a SQLite3 :: BusyException: database is locked error
What to do when you want to delete a migration file that is "NO FILE"
<f: ajax> Unable to attach <f: ajax> to non-ClientBehaviorHolder What to do when you become a parent
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
[Grails] Error occurred running What to do when the Grails CLI does not start
wildflly10 java8 ERROR [org.jboss.jca.core.tx.jbossts.XAResourceRecoveryImpl] (Periodic Recovery) IJ000906 What to do if an error occurs
What to do when is invalid because it does not start with a'-'
What to do if an error occurs in VS Code when importing a django module or your own module installed by pip install
What to do when The SSL certificate has expired
What to do when JSF tags do not become HTML
What to do if you get an "A server is already running." Error when you try to start the rails server
What to do if you accidentally create a model
ParseException: What to do when Unparseable date is reached
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do if you get an error saying "Please enter a valid value" when getting with Rails datetime_field
What to do if you get an error saying "Could not find a JavaScript runtime." When starting rails server
What to do when it becomes Unable to find CDI BeanManager.
What to do if you get a java.io.IOException in GlassFish
What to do when Method not found in f: ajax
What to do when you launch an application with rails
A validation error occurred when saving to the intermediate table.
What to do when Cannot apply expression operators to method binding
What to do when Cannot format given Object as a Date in convertDateTime of JSF
[Ruby / Rails] What to do when NoMethodError appears when using a destructive method such as filter!
What to do when a could not find driver appears when connecting to a DB in a Docker environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if you get a "302" error in your controller unit test code in Rails
[Rails] What to do when the view collapses when a message is displayed with the errors method