replace #write with FileUtils.copy_stream to prevent hitting memory limit