Uploading a file in to object storage container using node js application

In the earlier blog I’ve showed  how to generate authentication token for our object storage .In this blog I will show how to upload file in to object storage using that token.
So once we have our token we can use that to store files in the container.
In this blog I will create an api , using which we can upload a file in to object storage container.
For uploading any file through api , it is better that the file data should be passed to api as base64.Then it should be decoded and stored in object storage.

For uploading a file to objectstorage an api is provided which is,

API_ENDPOINT + ‘/’ + pdf_container_name + ‘/’ + pdfFileName
headers: {

‘X-Auth-Token’: token,

‘Content-Type’: ”

Here API_ENDPOINT is mentioned in the previous blog

Here token is the one which we generated and Content-Type depends on the file type.

First we will create a function which would accept the base64 data and the name with which it should be stored in object storage(eg: document.pdf)

function storepdf(basedata, pdfName) {
    var filename = pdfName;
    var promise = new Promise(function(resolve, reject) {
        var pdf_container_name = "accountdocs"; //containername in objectstorage
        var bufferString = basedata; 
        var pdfFileName = filename;              
        getAuthToken().then(function(token) {
                url: API_ENDPOINT + '/' + pdf_container_name + '/' + pdfFileName,
                method: 'put',
                headers: {
                    'X-Auth-Token': token,
                    'Content-Type': 'application/pdf'
            }, function(error, response, body) {
                if (!error && response.statusCode == 201) {
                    return (pdfFileName);
                } else {
                    return (error);

We are passing the base64 data as Stream and then we are decoding the base64 data and pipe() that data to our request.
Then in our main api we will call the above function as below :

app.post('/storepdf', function(req, res) {
    var data = req.body;
    var basedata = data.base64;
    var filename = data.filename;
    var fileUploadtime = (new Date).getTime();
    var file = filename.split(".")[0] + "_" + (new Date).getTime() + "." + filename.split(".")[1];
    storepdf(basedata, file);

Here we are appending timestamp to the filename ,storing the file in object storage and then we are sending that as response to this rest call.

Then we call use this api at our client side and server side and then upload your document.

Hope it helps someone.


One thought on “Uploading a file in to object storage container using node js application

  1. Pingback: Object storage:How to generate credentials and token using Node js application on bluemix – harishvarma

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s