Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno::EINVAL: Invalid argument @ io_write

Tags:

ruby

I have a piece of Ruby code that grabs a zip file from S3 and saves it locally.

File.open(local_filename, 'wb') { |file| file.write(body) }

This code works on some files but not others.

For the ones that don't work I get this error:

Errno::EINVAL: Invalid argument @ io_write - /path/to/file.zip

What could be the reason for this?

like image 993
Jason Swett Avatar asked Jan 26 '18 16:01

Jason Swett


1 Answers

The body might be too huge. The error message indeed doesn't make much sense, but it's a possible cause.


To mitigate this, write it in smaller chunks. The simplest (though not the fastest) way is going character by character:

File.open('file_name.txt', 'wb') do |file|
  body.each_char { |char| file.write char }
end
like image 79
ndnenkov Avatar answered Nov 05 '22 19:11

ndnenkov