Creating new database and user in mongodb on bluemix using Mongo CLI on windows

One we have Compose for Mongodb service on bluemix we want to create different users and add the permission levels according to database.Creating users directly from bluemix GUI is not possible.We need to use Mongo CLI for this.
On MAC,it is quite easy but on Windows it is difficult and we need to install SSL enabled mongodb to connect to mongodb on bluemix.

The software can be downloaded from the following link

Once the software is installed , open CLI and go to folder where mongodb is installed.
Then to connect to mongodb we need to paste uri_cli which is the ssl uri which u will find in credentials

mongossl.PNG

Then you will be connected to mongodb service on bluemix

mongocoonected

Now to see the available databases ,

showdbs

To create a new database
newdb.png
To create a new user and assign a role to the user use the following command

db.createUser(
  {
    user: "UserName",
    pwd: "Password",
    roles: [
       
       { role: "readWrite", db: "testdb" }
    ]
  }
);

usercreate

This way we can easily create user and assign permissions.To list the users use command >show users

Hope it helps someone!

Advertisements

Setup and Commands for Uploading/Downloading files from Objectstorage in Windows using Swift CLI

On Mac it is quite simple to upload and download files from object storage in bulk ,but on windows it is quite difficult since we dont have any available documentation for this.
I use Windows 8 and once I need to do a backup of all the files from object storage container .There are around 5000 pdf files and there is no option where we can directly download all these in a single go from object storage.We need to select one by one.
So then I thought of using Swift CLI and after long struggle I succeeded on windows.So hope this helps someone.

First we need to have Swift CLI set up on your local machine.
For that we need to install Python,PIP,Swift client and keystone client.

First we need to set up virtualenv since on windows it doesnt support the openstack version 3 and Object storage is based on V3 .
But fortunately, python provides a way to install various version of command via “virtualenv” mechanism. It is possible to run various version of python in a virtual environment. And surely it worked for Bluemix Object Storage service too.

First we need to download python on your local machine and then install it and set the path

For window python installation you can refer this

https://github.com/BurntSushi/nfldb/wiki/Python-&-pip-Windows-installation

Then we need to install pip. Even for that you can refer the above link.

Then open cmd in the browse to the folder where you have pip installed and then u need to install virtualenv

>pip install virtualenv

>virtualenv virt

Then go to virt folder location

>cd virt

Capture1

Then install python swift client,python-keystoneclient and ndg-httpsclient module.

>pip install python-swiftclient

>pip  install python-keystoneclient

>pip install –upgrade ndg-httpsclient

Capture2

Capture3

Environment is set now and we can use the swift commands.

Firsy we need to  set the environment variables to access the object storage which you can get those from object storage service credentials

osmask

The values like <user-id>, <password> and <project-id> can be obtained from Bluemix Object Storage service UI. Once the details are correct u are authenticated and connected to object storage.

Then we can see the containers list using the following command

swift list

Capture6

Below are the Swift commands for downloading and uploading

Download a particular file from container

>swift download <container-name> <file-name>

 

Download all the files with in a container

>swift download <container-name> .

 

Uploading file in to a container

Go to the folder location where you have these files

>swift upload <container-name> <filename>

 

Uploading all the files in a folder to a container

>swift upload <container-name>

 

 

 

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
METHOD : PUT
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) {
            getStringAsStream(bufferString).pipe(Base64.decode()).pipe(request({
                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);
    res.send(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.