Skip to main content
Version: 2.0.0

Sample: Sensitive Backup Storage

This tutorial presents a sample using Split.api. The concept and feature of this demo are to develop an automatic sensitive Backup Storage Application.

Introduction

We have data that we want to back up securely. The idea is simple once the data is created or copied in the temporary folder, it will be sent and stored securely with Astrachain's SpliT platform via secured API.

Once this file is uploaded, it will be deleted from the temporary folder. We can verify that it has been correctly saved via SPLiT web governance console, called cockpit.

schema.png

Video demonstration

In this section, you can see the demonstrative and explicative video of the demo.

Automatic Sensitive Data Backup

Requirements

To develop our sample we used:

  • Python Programming
  • Watchdog PyPI
  • HTTPS requests

SPLiT.API only accepts requests from registered users, so valid credentials (e-mail address and password) are required.

We started by creating a const.py file where we defined the api_url, and the valid credentials.

Authentication

In const.py, we create the credentials and the generic part of the api_url since we need to point to “auth/login” we added it.

import requests
from const import api_url, credential
headers = {"Content-Type": "application/json"}
responseConnexion = requests.request("POST", api_url+"auth/login", json=credential, headers=headers)

We also need to save the provided access token so that we can use it to identify the user for further actions.

jsonResponse = responseConnexion.json()
access_token= jsonResponse["accessToken"]
header = {'Content-Type': 'application/octet-stream; charset=utf-8' , 'accept':'application/json', 'Authorization': 'Bearer ' + access_token}

Get destination folder ID

In our case, we want to store all the files in a specific folder “HR_Backup” that we already created. We need to get the id of the specified folder.

First, we need the “rootDocumentId” since “HR_Backup” is created in the rootFolder.

responseUser = requests.request("GET", api_url+"users/me", headers=header)
userResponse= responseUser.json()
members= {}
members= userResponse["member"]
MemberRootDocumentId= members["rootDocumentId"]

Once we get the “rootDocumentId” we can get the Id of the specific folder.

responseList = requests.request("GET",api_url+"files/"+MemberRootDocumentId+"/children", headers=header)
listResponse = responseList.json()
for i in range (len(listResponse)):
if (listResponse[i]["name"] == "HR_Backup"):
idHrBackup = listResponse[i]["ID"]
break;
else:
idHrBackup = MemberRootDocumentId

Monitoring Temporary Folder

Once connected and knowing where we must send our documents, we can now focus on monitoring the temporary folder and detecting the creation of a file to backup it in a secure way.

We start by importing the needed libraries

import os
import time
from unicodedata import name
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from os import access

We define our class.

class MyEventHandler(FileSystemEventHandler):
def on_created(self, event):
print("created")
fileToSend=os.path.basename(event.src_path)
test_file = open(fileToSend, "rb").read()

responseFile = requests.post(api_url+"files/"+idHrBackup+"/children"+"?name="+fileToSend, headers=header, data= test_file)
if (responseFile.status_code == 200 or responseFile.status_code == 201):
os.remove(event.src_path)

Here is the main function.

if __name__ == "__main__":

event_handler = MyEventHandler()
path= "../TemporaryFolder"
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
print("Monitoring")
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
print("Done")
observer.join()