The reckoning , time to make it do something useful.

In the last post on the watcher script we created a basic function that monitored a folder and logged any new file creations to the folder.

In this post we're going to use that basic structure and add an extra element to write a copy of any new file added to a watched folder to an objectstore, in this case a Caringo endpoint.

First lets take a look at where we left off.

import sys
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class Event(FileSystemEventHandler):
    def on_created(self, event):
        filenamefromwatchdog = str(event.src_path)
        print(filenamefromwatchdog + "This came from our new class")



if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = Event()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while observer.is_alive():
            observer.join(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

So this code snippet checks the folder we specify via argv or the local folder and then it will simply print the file name on screen with an extra message to let us know it came from our new class.

lets add some code to upload a copy of newly added files to an object store that uses http. To do that we'll add the requests module which is a nice wrapper for urllib3

import sys
import logging
import requests

if you've not installed requests so far you can do that with pip3

pip3 install requests

The syntax for requests is very simple and easy to follow. But lets isolate it below and show it.

        # set the session up with the tokens etc
        # th
        s = requests.Session()
        cookies = {'token':  token }
        
        
        #earlier in the code we set two variables that 
        urlbucket=domain+bucket
        # here we pull the file name and open the file.
        filename = filenamefromwatchdog
        uploaddata = {'file': open(filenamefromwatchdog, 'rb')}
        # here we do the post.
        r = s.post(urlbucket + filename, data=open(filename, 'rb').read(), headers=content, cookies=cookies)

By Tony Lokko