Rails S3 upload error—IOError: closed stream

1k views Asked by At

I have this code:

file = File.open(model_file_path, 'w') do |f|
  f << YAML.dump(attributes)
end

s3_bucket.object(filename).upload_file(file)

And it's throwing this error:

IOError: closed stream
    from /var/www/my_app/shared/bundle/ruby/2.4.0/gems/aws-sdk-resources-2.10.126/lib/aws-sdk-resources/services/s3/file_uploader.rb:31:in `size'
    from /var/www/my_app/shared/bundle/ruby/2.4.0/gems/aws-sdk-resources-2.10.126/lib/aws-sdk-resources/services/s3/file_uploader.rb:31:in `upload'
    from /var/www/my_app/shared/bundle/ruby/2.4.0/gems/aws-sdk-resources-2.10.126/lib/aws-sdk-resources/services/s3/object.rb:252:in `upload_file'
    from (irb):44

What's causing the error? Am I saving it wrong?

1

There are 1 answers

0
Mirror318 On

The file needs to be in an open state to be uploaded:

file = File.open(model_file_path, 'w') do |f|
  f << YAML.dump(attributes)
end

File.open(model_file_path, 'r') do |f|
  s3_bucket.object(filename).upload_file(f)
end