How to use the anynines Swift service with your app


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:

require 'json'
def get_swift_credentials_hash
vcap_services_env_str = ENV["VCAP_SERVICES"]
vcap_services_hash = hash = JSON.parse vcap_services_env_str

# We are able to access the credentials from a simple hash now.
# The next step describes the fog configuration hash
# which is implemented within the following method

def fog_credentials_from_cf_swift_credentials(cf_swift_credentials)
:provider => ‘HP’,
:hp_access_key => cf_swift_credentials[“user_name”],
:hp_secret_key => cf_swift_credentials[“password”],
:hp_tenant_id => cf_swift_credentials[“tenant_id”],
:hp_auth_uri => cf_swift_credentials[“authentication_uri”],
:hp_use_upass_auth_style => true,
:hp_avl_zone => cf_swift_credentials[“availability_zone”],
:hp_auth_version => cf_swift_credentials[“authentication_version”].to_sym,
:os_account_meta_temp_url_key => cf_swift_credentials[“account_meta_key”],
:hp_service_type => “object-store”


!!! Attention !!!

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'

h = get_swift_credentials_hash
fog_options = fog_credentials_from_cf_swift_credentials(h)
connection =

Fog usage

Fog provides a simple interface for manipulating files and directories on a cloud file storage.

The following code snippets demonstrate the basic usage of fog:

# create a directory
dir = connection.directories.create(:key => "my_directory_name")

# upload a file
dir = connection.directories.get(“my_directory_name”)
dir.files.create(:key => “example_file.txt”, :body =>“/path/to/example_file.txt”))

# list the directory’s contents
files = dir.files
puts files.to_s

# delete the file again
file = dir.files.get(“example_file.txt”)

# delete the directory again

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:

# create a directory and set it to private access
dir = connection.directories.create(:key => "my_private_dir")
dir.public = false

# upload a file to the directory
file = dir.files.create(:key => “example_file.txt”, :body =>“/path/to/example_file.txt”))

# 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:

a) Deploying a new or updating an existing app

$>cd my/app/directory
$>cf push
Name> app_id

Instances> 1

1: 64M
2: 128M
3: 256M
4: 512M
5: 1G
Memory Limit> 2

Creating app_id… OK

1: app_id
2: none
Subdomain> app_id

2: none

Creating route… OK
Binding to app_id… OK

Create services for application?> y

1: mongodb 2.0
2: mysql 5.5
3: postgresql 9.0
4: rabbitmq 2.8
5: redis 2.2
6: swift 1.0
What kind?> 6

Name?> swift-22fc6

1: free: free plan
Which plan?> 1

Creating service swift-22fc6… OK
Binding swift-22fc6 to app_id… OK
Create another service?> n

Bind other services to application?> n

Save configuration?> n

Uploading app_id… OK
1/1 instances: 1 running

b1) Binding a running application to the Swift service

Create a service instance first:

$>cf create-service
1: mongodb 2.0
2: mysql 5.5
3: postgresql 9.0
4: rabbitmq 2.8
5: redis 2.2
6: swift 1.0
What kind?> 6

Name?> my_swift

1: free: free plan
Which plan?> 1

Creating service my_swift… OK

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.

Rails & Swift Example App

If you would like to dive deeper into ruby code or test the deployment process with an example app, please refer to our Swift Rails example application:

Stay tuned for further service additions to the anynines Platform as a Service…

One thought on “How to use the anynines Swift service with your app

  1. Pingback: Scalable Swift File Storage Service | anynines blog

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>