Mark III Systems Blog

Using IBM Cleversafe Object Storage Through a Proxy with Node.js

 

If you have to work with an “on premise” instance of Cleversafe, chances are there is a firewall that you must go through. If you have your app running on a service such as Bluemix where you don’t have control over which IP the server uses to make the requests, this guide might be useful for you.

You can click here to use this tutorial on how to set up a static IP to your Bluemix app to set up a Statica proxy. Once you have that set up you will have a URL that will contain your credentials.

Now that you have your proxy set up, you will need your IT team to whitelist the static IP’s given to you by Statica. Now lets see some sample code to access Cleversafe through the proxy:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//We will use the request module to make the calls to Cleversafe
//and the fs-extra to read the files from the local storage

var request = require('request');
var fs = require('fs-extra');


var statica = "http://<Your Statica URL goes here>"

//cleversafe Credentials
var username = '<cleversafe username>';
var pass = '<cleversafe password>';

//BASIC authentication method
var auth = 'Basic ' + new Buffer(username + ':' + pass).toString('base64');


//Set up proxy and method to deposit files
var options = {
url: 'http://<Cleversafe address>/<Bucket name>/<filename>',
proxy:statica,
port: 80,
method: 'PUT',
headers: {
'Authorization':auth,
'User-Agent': '<User agent of your choice>'
}
};



function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}


}

//Deposit your file to Cleversafe
fs.createReadStream('<filename>').pipe(request(options, callback));

 

Now a bit of explaining.

The Statica proxy settings are being set inside the “options” object, we are setting the proxy for the request by using the url as the value for the field “proxy”, this will tell the request module to send all the requests through Statica and that way we can access Cleversafe even behind the firewall.

The Cleversafe authorization we are using is the BASIC method. This method consists of a string in the form “Basic <username>:<password>” that is then turned into a base64 string, Cleversafe is also compatible with other methods of authentication but in this case, BASIC worked just fine for me. The authorization goes inside the headers object by setting the field “Authorization” to the hashed string that comes from line 15.

In order to retrieve objects from Cleversafe, you just need to change the method from PUT to GET. Here is an example where the server works as an intermediary between the client and Cleversafe to return an image to the client:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
app.get('/cleverget/:filename', function(req,res) {
    console.log("getting.."+req.params.filename+ ' from CleverSafe');
    

	var options = {
		url: 'http://<Cleversafe address>/<Bucket name>/<filename>',
		proxy:statica,
		port: 80,
		method: 'GET',
		headers: {
		'Authorization':auth,
		'User-Agent': '<User agent of your choice>'
		}
	};



	function callback(error, response, body) {
	if (!error && response.statusCode == 200) {
	console.log(body);
	}


	}

	//in this case the example is an image
	res.writeHead(200, {'Content-Type' : 'image/png'});
	
	//pipe back the image to the client
	request(options, callback).pipe(res);

});

This example requires Express to be properly set up. A very good thing is that we are not storing the image but simply sending it to the client via piping. That way, the client does not get direct access to Cleversafe and there is no need to set up the proxy or client credentials for it.

These are just 2 basic operations of Cleversafe, there are many more to be used. Don’t be afraid to experiment and modify the requests to make it fit your needs. Feel free to comment or send us any questions you might have!

Guest blog from BlueChasm!