Topic: How to us FOG

I'm a newbie.
I would like to use FOG in connection with Amazon S3.
The instruction say to use this for the connection:
--------------------------
require 'rubygems'
require 'fog'

# create a connection
connection = Fog::Storage.new({
  :provider                 => 'AWS',
  :aws_secret_access_key    => YOUR_SECRET_ACCESS_KEY,
  :aws_access_key_id        => YOUR_SECRET_ACCESS_KEY_ID
-----------------------
Where do you put this? In the controller?
does it need a def tag?

Thanks,
Reddirt

Last edited by reddirt (2012-02-06 18:53:11)

Re: How to us FOG

It depends,  it could go in the controller,  in the action that will require access to Fog

Say you have a model of some kind,  and each record in the model has some associated data in the cloud somewhere,  and you wanted it display when you show all of your records for that model,  then you would put it the index action of the corresponding controller, i.e.

app/controllers/foo_controller.rb:

def index
  @cloudstuff = []
  connection = Fog::Storage.new({
    :provider                 => 'AWS',
    :aws_secret_access_key    => YOUR_SECRET_ACCESS_KEY,
    :aws_access_key_id        => YOUR_SECRET_ACCESS_KEY_ID
    })
  @foos = Foo.all()
  @foos.each do |f|
    @cloudstuff << connection.whatever(f.foo_related_argument)
  end
end

But, that's a design choice.   It could go in any controller action where you need it.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to us FOG

Brad,
Thanks for the input.

If I wanted to list the files in the S3 directory ( connection.directories),
how would I code that in the html.erb file?

Thanks,
Reddirt

Re: How to us FOG

I'm going to guess, because I don't really know the Fog api

To be pure to the rails way,  you extract the directories in the controller,  maybe something like

@s3dirs = connection.directories

Then in the view

<% @s3dirs.each do |s|
  <%= s.dir_name %>
<% end %>

Last edited by BradHodges (2012-02-07 01:20:37)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to us FOG

Brad,
Thanks - I was able to list the directories.

Reddirt