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