This commit is contained in:
Tyler Perkins 2022-12-10 15:16:43 -05:00
parent 26a585ee2c
commit 57cc50a44c

85
main.py Executable file
View File

@ -0,0 +1,85 @@
#!/usr/bin/python
import time
import sys
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from minio import Minio
from minio.commonconfig import Tags
from minio.error import S3Error
DIRECTORY_TO_WATCH = "/media/hd/memes/"
client = None
S3_BUCKET = None
def uploadFile(file_name):
pass
class Watcher:
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error!")
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
None
elif event.event_type == 'created':
print("Made file " + event.src_path)
tags = Tags.new_object_tags()
tags["uploader"] = "Tyler"
head, filename = os.path.split(event.src_path)
client.fput_object(bucket_name=S3_BUCKET,
object_name=filename,
file_path=event.src_path,
tags=tags,
content_type=None)
os.remove(event.src_path)
print("Uploaded file!")
def main():
global client
global S3_BUCKET
if "S3_URL" not in os.environ:
raise Exception("S3_URL is not set!")
S3_URL = os.environ["S3_URL"]
if "S3_UN" not in os.environ:
raise Exception("S3_UN is not set!")
S3_UN = os.environ["S3_UN"]
if "S3_PW" not in os.environ:
raise Exception("S3_PW is not set!")
S3_PW = os.environ["S3_PW"]
if "S3_BUCKET" not in os.environ:
raise Exception("S3_BUCKET is not set!")
S3_BUCKET = os.environ["S3_BUCKET"]
# override defaults
if "S3_TLS" in os.environ:
S3_TLS = os.environ["S3_TLS"].lower() in ("yes", "true", "1", "t")
client = Minio(S3_URL,
access_key=S3_UN,
secret_key=S3_PW,
secure=S3_TLS)
w = Watcher()
w.run()
if __name__ == '__main__':
sys.exit(main())