Object storage:How to generate credentials and token using Node js application on bluemix

Object Storage

It is one most useful service if you are developing applications on bluemix .It provides unstructured cloud data storage. You can store and access your content using this service.

You can store your files such as pdf,txt,mp3 etc. Also you can connect to another apps and services.

How can we store files

First thing you need to have a bluemix account.Then login to your account and select your particular space.In data and analytics you can find object storage service and create it.Creating a service is nothing but an instance of that service getting created in your space.Once you create your service, create a container. This can be easily done from Manage tab.Below image shows it

 Creating a container

Once the container is created you can directly upload your files using the bluemix GUI.But in the real time we need our files to be uploaded through our application may be using some rest apis..

How can we use this service through application ?

IBM Object storage is based on SWIFT api . The service uses OpenStack Identity (Keystone) for authentication and can be accessed directly by using OpenStack Object Storage (Swift) API v1 calls.
So first we need some authentication keys to be generated for this service to use through our application.

Creating service keys for authentication:

Login to your bluemix account using cloud foundry using cf login

Then use the following command:

         cf create-service-key Objectstorage_servicename My-Service-Key

 

 

 

Now we will list this credentials since we need this by our application to authenticate to the object storage.

cf service-key My-Object-Storage My-Service-Key

 

 

 

 

Once we have these details we can focus on our application.Object storage is developed in such a away that if we want to use any api it requires an authentication token which is generated using the above values.

So the most important thing is generating a token.To generate this token we have an api for  region specific where we need to perform a post call with auth details of userid,password,project_id
Below is the api for UK region and for US region we need to replace lon with dal

  https://lon-identity.open.softlayer.com/v3/auth/tokens

Using the generated token we can upload or download any file from the container.
Generating token through post man or soap ui is simple.But we need to generate this using our node js application.Below code snippet shows you how we can generate that using our node js application.

Generating this token using NODE JS Application

// Create a config object
var config = {};
// Specify Openstack as the provider
config.provider = "openstack";
// Authentication url
config.authUrl = "https://lon-identity.open.softlayer.com";
config.region = "london";
// Use the service catalog
config.useServiceCatalog = true;
// true for applications running inside Bluemix, otherwise false
config.useInternal = true;
// projectId as provided in your Service Credentials
config.tenantId = "8fa4009784be465fa601ddc998a05ba0";
// userId as provided in your Service Credentials
config.userId = "b30caec05e054063b3ac52ec4844586c";
// username as provided in your Service Credentials
config.username = "admin_c62585ab7f8d4322654d09db3871c0195389040a";
// password as provided in your Service Credentials
config.password = "o()V]9=tz)~.d7Mu";
var API_ENDPOINT = ['https://lon.objectstorage.open.softlayer.com/v1/AUTH_', config.tenantId].join('');

config.auth = {
    forceUri: "https://lon-identity.open.softlayer.com/v3/auth/tokens",
    interfaceName: "public",
    "identity": {
        "methods": [
            "password"
        ],
        "password": {
            "user": {
                "id": "b30caec05e054063b3ac52ec4844586c", //userId
                "password": "o()V]9=tz)~.d7Mu" //userPassword
            }
        }
    },
    "scope": {
        "project": {
            "id": "8fa4009784be465fa601ddc998a05ba0"
        }
    }
};
//console.log("config: " + JSON.stringify(config));
var getAuthToken = function() {
    if (AUTH_TOKEN === '') {
        return new Promise(function(resolve, reject) {
            var authUrl = config.authUrl;
            var authInfo = {
                auth: {
                    identity: {
                        methods: ['password'],
                        password: {
                            user: {
                                id: config.userId,
                                password: config.password
                            }
                        }
                    },
                    scope: {
                        project: {
                            id: config.tenantId
                        }
                    }
                }
            }
            request({
                url: authUrl  + '/v3/auth/tokens',
                method: 'post',
                body: authInfo,
                json: true
            }, function(error, response, body) {
                if (error)		
                    reject(error);
                else {
                    resolve(response.headers['x-subject-token']);
                }
            });
       });
    } else {
        console.log("AUTH_TOKEN" + AUTH_TOKEN);
        return AUTH_TOKEN;
    }
}

Once the token is generated we can interact with the object storage through our node js application. In the next blog I will show how to use this token to upload file in to object storage container through our app.

Hope it helps someone!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s