简体   繁体   中英

Uploading a file to Facebook using Koala on Ruby on Rails

I followed the following blogpost to figure out how to create Facebook events remotely using my app. I've been having problems loading the images from my app, however, because I do not have images stored locally on my app, they are stored in AWS.

  @graph = Koala::Facebook::GraphAPI.new(@token) picture = Koala::UploadableIO.new(@event.photo.url(:small)) params = { :picture => picture, :name => 'Event name', :description => 'Event descriptio :start_time => datetime, } 

is the following code I am currently using to send pictures to Facebook when Facebook events are created on my app. The problem is, however, that Rails is throwing the error: No such file or directory - http://s3.amazonaws.com/ColumbiaEventsApp/photos/21/small.jpeg?1312521889 .

Does anybody who's more experienced with Rails development know if there is a way for me to treat a URL like a path to a file? The UploadableIO class expects a path to a file, and I'm struggling to figure out if there's a way in Ruby to treat URL's like filepaths. The way that photos stored on the app can be loaded to Facebook is as follows:

picture = Koala::UploadableIO.new(File.open("PATH TO YOUR EVENT IMAGE"))

if that helps.

I appreciate any new insights into this issue.

Ok so I played around and figured out how to post pictures.

Basically what I did was use the 'open-uri' library to convert the image links into file objects, which can then be passed to UploadableIO and sent to Facebook. This is the code that worked:

require 'open-uri'
OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax')
OpenURI::Buffer.const_set 'StringMax', 0
picture = Koala::UploadableIO.new(open(@event.photo.url(:small)).path, 'image')
params = {
  picture:     picture,
  name:        @event.name,
  description: @event.description,
  location:    @event.location,
  start_time:  datetime
}
@graph.put_object('me', 'events', params )

The OpenURI constant StringMax needed to be changed because the image files I was using were small enough that the files were being processed as Strings rather than File Objects.

Hope this helps anyone trying to fix this!

With Koala 1.2.1 it's a very elegant solution. Here is sample code for creating an album and uploading to it from a remote, AWS link (btw this took about 30 lines in PHP w/ the PHP SDK!

@foo = Foo.find(params[:foo_id])        
albuminfo = @graph.put_object('me','albums', :name=>@foo.title)
album_id = albuminfo["id"]    
@graph.put_picture(@foo.remote_image_path,{}, album_id)

For Facebook Ad Images , you unfortunately currently cannot do it by URL, thus:

require 'open-uri'
img_data = open(my_post.image.url :medium).read
img = graph.put_connections('act_X', 'adimages', bytes: Base64.encode64(img_data))

Facebook recently released an update that lets you post pictures using publicly accessible URLs ( http://developers.facebook.com/blog/post/526/ ). The Koala library you're using supports that ( https://github.com/arsduo/koala/blob/master/lib/koala/graph_api.rb#L102 ), so you should be able to post the pictures you're hosting on S3 without having to use OpenURI::Buffer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM