-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
68 lines (53 loc) · 1.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import time
import boto3
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
s3_bucket_name = ""
filename = ""
class OnMyWatch:
# Set the directory on watch
watchDirectory = "e:/Code/indellinent/test/"
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.watchDirectory)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Observer Stopped")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.event_type == 'created' and filename in event.src_path :
s3 = boto3.client(
's3',
region_name = os.environ['AWS_REGION'],
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
)
s3.upload_file(event.src_path, s3_bucket_name, filename)
print("Watchdog received created event - % s." % event.src_path)
elif event.event_type == 'modified' and filename in event.src_path :
s3 = boto3.client(
's3',
region_name = os.environ['AWS_REGION'],
aws_access_key_id = os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key = os.environ['AWS_SECRET_ACCESS_KEY']
)
s3.upload_file(event.src_path, s3_bucket_name, filename)
print("Watchdog received modified event - % s." % event.src_path)
else:
return None
if __name__ == '__main__':
#Set global variables as data that's passed from command line
s3_bucket_name = sys.argv[1]
filename = sys.argv[2]
watch = OnMyWatch()
watch.run()