How to download and delete file in Cloudant Nosql DB using python-flask?

python cloudant
If you have already read our article on how to upload a file in cloudant NoSql DB, then consider this the next part of that article where we will be talking about how to download and/or delete a file in cloudant using python. If you have not gone through our previous article, click here.

Let’s jump directly into establishing a route for downloading a file or attachment from the NoSQL database in cloudant.

Downloading

@app.route('/download', methods=['POST'])
def download():

As you can see in our index file shown in the previous article, we take filename as input and submit it to “/download”.
Let’s receive that input filename to check if it is in our database or not.

file_name = request.form['filename']

Now it’s time to check if the file exists in the database. If it does, we will progress further.
We will do this my seeing if file_name input matches any document with the same value in “file_name” attribute.

for document in my_database:
        if document['file_name'] == file_name:

To download the content of the attachment, we will have to use the get_attachment method

file = document.get_attachment(file_name, attachment_type='binary')

We have to convert the return value to a proper response object. For this we use make_response method.
Then we give header to the response by using response.headers method.

response = make_response(file)
response.headers['Content-Disposition'] = 'attachment; filename=%s'%file_name

To actually download the attachment we will have the return the response

return response

The overall code would be something like this:

@app.route('/download', methods=['POST'])
def download():
    file_name = request.form['filename']
    for document in my_database:
        if (document['file_name'] == file_name):
            file = document.get_attachment(file_name, attachment_type='binary')
            response = make_response(file)
            response.headers['Content-Disposition'] = 'attachment; filename=%s'%file_name
            return response
        else:
            response = 'File not found'
    return response

Deleting

Deleting the attachment is similar and very easy.
As in upload and download, you create a new route for delete and then receive the filename

@app.route('/delete', methods=['POST'])
def delete():
    file_name = request.form['filename']

Again, as we did in download, we search for a document that has the file_name stored in ‘file_name’ attribute.

for document in my_database:
    if (document['file_name'] == file_name and document['version'] == int(file_version)):

To delete the attachment, it is easy just to delete the document since we were creating new document for each file upload.

document.delete()

OR we could use

document.delete_attachment(file_name)

This could delete only the attachment from the document.

The overall code would look something like this:

@app.route('/delete', methods=['POST'])
def delete():
    file_name = request.form['filename']
    for document in my_database:
        if document['file_name'] == file_name:
            print('File found and deleted')
            document.delete()
            #document.delete_attachment(file_name)
        else:
            print ('File not found')
    return app.send_static_file('index.html')

Here we go! We have learned how to download and delete file in cloudant Nosql database (and upload) using python and flask.