2011-03-31 14:22:02 -04:00
---
layout: default
2011-04-01 09:24:50 -04:00
title: Getting Started
2011-03-31 14:22:02 -04:00
---
2011-04-01 14:20:47 -04:00
First off, install the gem:
2011-03-31 14:22:02 -04:00
$ gem install fog
2011-04-01 14:20:47 -04:00
## Setting Up Local Storage
2011-03-31 14:22:02 -04:00
2011-05-05 17:34:53 -04:00
We will be using Local storage in the example. Local storage provides the same api that cloud storage services in fog do, but without the bother of needing to signup for stuff or pay extra money.
First, make a local directory to hold your data.
2011-03-31 14:22:02 -04:00
$ mkdir ~/fog
2011-05-05 19:32:24 -04:00
Now we can start writing our script, first off we should require fog.
require 'rubygems'
require 'fog'
2011-05-05 17:34:53 -04:00
Now in order to play with our data we need to setup a storage connection.
2011-03-31 14:22:02 -04:00
2011-05-27 15:02:22 -04:00
storage = Fog::Storage.new({
2011-04-01 14:20:47 -04:00
:local_root => '~/fog',
2011-09-26 16:46:08 -04:00
:provider => 'Local'
2011-05-27 15:02:22 -04:00
})
2011-05-05 17:34:53 -04:00
`storage` will now contain our storage object, configured to use the Local provider from our specified directory.
## Storing Data
Now that you have cleared the preliminaries you are ready to start storing data. Storage providers in fog segregate files into `directories` to make it easier to organize things. So lets create a directory so we can see that in action.
2011-09-26 16:46:08 -04:00
directory = storage.directories.create(
2011-05-05 17:34:53 -04:00
:key => 'data'
)
To make sure it was created you can always check in your filesystem, but we can also check from inside fog.
storage.directories
Progress! Now it is time to actually create a file inside our new directory.
file = directory.files.create(
:body => 'Hello World!',
:key => 'hello_world.txt'
)
We should now have our file, first we can open it up and make sure we are on the right track.
$ open ~/fog/hello_world.txt
It is much more likely that you will want to see what files you have from inside fog though.
directory.files
Now that we have run through all the basics, lets clean up our mess.
file.destroy
directory.destroy
After that you should be able to check your directory list in fog or your filesystem and see you are safely back to square one.
## Next Steps
2011-11-09 14:33:51 -05:00
Using the same interface you can also practice working against a real provider (such as Amazon S3). Rather than worrying about signing up for an account right away though, we can use mocks to simulate S3 while we practice.
2011-05-05 17:34:53 -04:00
This time we will turn on mocking and then, just like before, we will need to make a connection.
Fog.mock!
2011-05-27 15:02:22 -04:00
storage = Fog::Storage.new({
2011-05-05 17:34:53 -04:00
:aws_access_key_id => 'fake_access_key_id',
:aws_secret_access_key => 'fake_secret_access_key',
:provider => 'AWS'
2011-05-27 15:02:22 -04:00
})
2011-05-05 17:34:53 -04:00
2011-11-09 14:33:51 -05:00
You may notice that we used bogus credentials, this is fine since we are just simulating things. To use real S3 you can simply omit Fog.mock! and swap in your real credentials.
2011-05-05 17:34:53 -04:00
2011-11-09 14:33:51 -05:00
If you'd like to turn off mocking after turning it on, you can do it at any time and every subsequent connection will be a real connection.
# Turn on mocking
Fog.mock!
# Create a mock connection to S3
storage = Fog::Storage.new({
:aws_access_key_id => "asdf",
:aws_secret_access_key => "asdf",
:provider => "AWS"
})
# Turn off mocking
Fog.unmock!
# Create a real connection to S3
storage = Fog::Storage.new({
:aws_access_key_id => "asdf",
:aws_secret_access_key => "asdf",
:provider => "AWS"
})
Don't worry about your losing mock data, it stays around until you reset it or until your process exits.
# Reset all mock data
Fog::Mock.reset
2011-05-05 17:34:53 -04:00
Congratulations and welcome to the cloud! Continue your journey at [fog.io ](http://fog.io )