0

I am trying to download images from the web and upload them back to Cloudinary. The code I have works for some images, but not for others. I have isolated the problem down to this line (it requires open-uri):

image = open(params[:product_image][:main])

For this image, it works fine. image is

#<Tempfile:/var/folders/49/bmhbmmzj5fl31dm9j6m6gxr00000gn/T/open-uri20150526-7662-1b676ws>

and cloudinary accepts this. However, when I try to pull this image, image becomes

#<StringIO:0x007fa0267c8f80 @base_uri=#<URI::HTTP:0x007fa0267c92c8 URL:http://www.spiresources.net/WebImages/480/swatch/CELW.JPG>, 
@meta={"date"=>"Tue, 26 May 2015 22:17:47 GMT", "server"=>"Apache/2.2.22 (Ubuntu)", 
"last-modified"=>"Mon, 29 Jun 2009 00:00:00 GMT", "etag"=>"\"44700f-c35-46d715f090000\"", 
"accept-ranges"=>"bytes", "content-length"=>"3125", "content-type"=>"image/jpeg"}, @metas={"date"=>["Tue, 26 May 2015 22:17:47 GMT"], "server"=>["Apache/2.2.22 (Ubuntu)"], 
"last-modified"=>["Mon, 29 Jun 2009 00:00:00 GMT"], "etag"=>["\"44700f-c35-46d715f090000\""], "accept-ranges"=>["bytes"], 
"content-length"=>["3125"], "content-type"=>["image/jpeg"]}, @status=["200", "OK"]>

which cloudinary rejects and raises an error of "No conversion of StringIO to string". Why does open-uri return different objects for what would seem like similar images? How can I make open-uri return a tempfile or at least turn my StringIO to a tempfile?

Ryan K
  • 3,985
  • 4
  • 39
  • 42

1 Answers1

2

You can simply give the URL to the Cloudinary upload method. Then Cloudinary will fetch the remote resource directly.

Tal Lev-Ami
  • 1,417
  • 10
  • 10