How to use the anynines Swift service with your app

Introduction

The anynines Swift service enables your apps to store files to our Swift cloud storage solution. This tutorial will demonstrate the usage of the Swift service from within ruby apps using the fog gem. Fog implements a wrapper around cloud storage services and includes provider (driver) classes for many different storage providers. Currently the hp provider is the most compatible driver for our Swift service. Please ensure that you are using fog version 1.15.0 to use the hp provider’s temp url creation function.

Let’s step into the storage arena!

Configuration (Initialize the swift credentials)

Anynines services use a well defined system for generating and assigning credentials to apps that intend to use the services’ resources. The process of assigning user credentials to apps is called binding within the cloud foundry platform. When a service is bound to an app the app’s execution environment variables contain the user credentials needed to access the service’s resources. So we have to read the VCAP_SERVICES environment variable for gathering the login information for the hp fog provider. The following code snippet demonstrates this process:

Please use hp_service_type: “object-store” for fog versions >= 1.24.0 and “Object Storage” for previous versions. We created the a9s_swift gem to ease the usage of the anynines Swift service. Just have a look at one of our blog posts about Carrierwave and Paperclip configuration using the a9s_swift gem or dive into our a9s_swift usage examples.

Initialize the fog connection

# The retrieved hash can be used for creating
# a fog connection to the Swift storage
require 'fog'

# delete the directory again
connection.directories.get(“my_directory_name”).destroy

In addition to the basic functions above, fog provides the ability to define directories access rights as public or private. Private directories and their contents can just be accessed by autorized users. Fog can generate temporary urls for private files which offers a public link to them for a defined limit of time. The next code snippet demonstrates these more advanced functions:

# generate a temporary url that expires in 600 seconds for the file
# this function was newly introduced within fog 1.15.0, so please ensure you have to have the newest version of fog installed
url = file.temp_signed_url 600, “GET”
puts url

Service Binding

Let’s say we are finished with integrating fog into our ruby app. How can we now connect anynines’ Swift service to our app?
The cf command line tool makes it really easy to bind our app and enable it to access the cloud file storage.
There are two possible scenarios when it comes to service bindings:

b2) Bind the instance to your application:

$>cf bind-service
1: swi
2: app_id
Which application?> 2

1: mysql-32ef6
2: my_swift
Which service?> 2

Binding my_swift to app_id… OK
Creating service my_swift… OK

If your app doesn’t need a service any longer you can use the ‘cf unbind-service’ command to unbind a service from your app.
These simple commands are all you need to connect your app to the Swift storage. No further complicated installation or administration is needed.