Using fog-softlayer for Managing Object Storage

In part two of this series we explored using fog-softlayer to leverage fog, the cloud services library, for managing servers.

In this installment, we will cover several examples of using fog-softlayer to manage SoftLayer Object Storage.

Configure fog-softlayer for your account.

These examples assume you have ~/.fog which contains:

   :softlayer_username: example-username
   :softlayer_api_key: 1a1a1a1a1a1a1a1a1a11a1a1a1a1a1a1a1a1a1 
   :softlayer_cluster: cluster # currently supported clusters are dal05, sng01, ams01
 

Create a Connection to SoftLayer Object Storage

 require 'fog/softlayer'
    @sl = Fog::Storage[:softlayer]

Models are Pretty

As mentioned in part two, fog has a fantastic object model for working with clouds to provision infrastructure. Your primary interface for working with fog should be the models. We will explore using fog-softlayer to make requests directly in a future post.

Let's start at the beginning.

1. Create a directory/container.

   @sl.directories.create(:key => 'a-container')
 

Try creating a couple more with different names so that you have something to look at when you run the next few examples.

The next step explains how to look at the containers we have.

2. List directories/containers.

    dirs = @sl.directories
    dirs.size # the number of directories      
 

Getting a single container will include a summary of its contents.

3. Get a directory/container.

   dir = @sl.directories.get('a-container')
    dir.key  # => 'a-container'
 

Now, let's add some contents.

4. Create a new file/object.

   dir = @sl.directories.get('a-container')
    # Pass a string.
    dir.files.create(:key => 'data.txt', :body => 'The quick brown fox jumps over the lazy dog.")
    # From a file.
    dir.files.create(:key => 'file-data.txt', :body => File.open('/path/to/file-data.txt')
  

Now that we've done that, we can pull it back down ...

5. Get an existing file/object.

   dir = @sl.directories.get('a-container')
    file = dir.files.get('data.txt')
    file.body # => 'The quick brown fox jumps over the lazy dog.'
 

... or we can make a copy of it.

6. Copy a file/object.

   file  = @sl.directories.get('a-container').files.get('data.txt')
    copy = file.copy('a-container', 'copy-of-data.txt')
    copy.body # => 'The quick brown fox jumps over the lazy dog.'
 

7. List the files in a directory/container.

   @sl.directories.get('a-container').files
   # => [
   #    <Fog::Storage::Softlayer::File
   #  key="a-container/data.txt",
   #  content_length=43,
   #  content_type="text/plain",
   #  content_disposition=nil,
   #  etag="a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1",
   #  last_modified=1970-00-00 00:00:00 -0000,
   #  access_control_allow_origin=nil,
   #  origin=nil
   #  >
   # ...
 

SoftLayer's object storage allows you to create signed URLs that expire when you want them to.

8. Get a signed [temporary] URL for a file/object.

    file = @sl.directories.get('a-container').files.get('data.txt')
      file.url(Time.now + 300) # url expires in 5 minutes
 

Let's clean up these tests so they're not leaving behind clutter.

9. Delete files/objects from a directory/container.

   dir = @sl.directories('a-container')
    dir.files.get('data.txt').destroy
    dir.files.get('file-data.txt').destroy
    dir.files.get('copy-of-data.txt').destroy
    # Must destroy all files/objects before destroying container.
    dir.destroy
 

Try Before You Buy

One of the excellent features included in fog-softlayer (and for all supported providers) is fantastic mocking.

Even if you don't have a SoftLayer account, you can download fog-softlayer and start testing this code.

Just call Fog.mock! immediately after requiring the gem, and you can run all of the above code examples and see how easy it is to work with. We're working hard to get full coverage of Mock classes and methods. You may run across a MockNotImplemented exception here or there. If you do, probably the best course is to fork the repo, implement the mock, and open a pull request.

- Matt

--

If you have questions about or problems with fog-softlayer open an issue or email me.