cloudinary / cloudinary_gem

Cloudinary GEM for Ruby on Rails integration
https://cloudinary.com
420 stars 285 forks source link

Unable to upload multiple images to cloudinary #500

Closed avosa closed 1 year ago

avosa commented 1 year ago

Describe the bug in a sentence or two.

When upload images simultaneously i encounter IOError with the message Closed stream.

Sample code looks like below


def save_images
  resized_images = {
    img_64: {
      :transformation => [
        {:width => 64, :height => 64, :crop => :limit}
      ]
    },
    img_128: {
      :transformation => [
        {:width => 128, :height => 128, :crop => :limit}
      ]
    },
    img_256: {
      :transformation => [
        {:width => 256, :height => 256, :crop => :limit}
      ]
    }
  }
  response_1 = Cloudinary::Uploader.upload(@video.thumbnail, :transformation => resized_images[:img_64][:transformation])
  @video.update(img_64: response["secure_url"])

  response_2 = Cloudinary::Uploader.upload(@video.thumbnail, :transformation => resized_images[:img_128][:transformation])
  @video.update(img_128: response["secure_url"])

  response_3 = Cloudinary::Uploader.upload(@video.thumbnail, :transformation => resized_images[:img_256][:transformation])
  @video.update(img_256: response["secure_url"])
end

end
avosa commented 1 year ago

I managed to solve the problem! This guide was all I needed!

Apparently, after every upload, the file is closed(I was trying to have the same file uploaded and transformed into different versions and the first transformation was the only one that was successful. To make the second and third transformation, I had to open the file again as it get closed once the operation is complete.

Here's how I fixed it:

response_1 = Cloudinary::Uploader.upload(@video.thumbnail, :transformation => resized_images[:img_64][:transformation])
  @video.update(img_64: response["secure_url"])

  response_2 = Cloudinary::Uploader.upload(@video.thumbnail.open, :transformation => resized_images[:img_128][:transformation])
  @video.update(img_128: response["secure_url"])

  response_3 = Cloudinary::Uploader.upload(@video.thumbnail.open, :transformation => resized_images[:img_256][:transformation])
  @video.update(img_256: response["secure_url"])

As you can see, I only needed to add .open method to the second and third transformation.

Extra resources/guide can be found here

I hope this helps somebody!