Created
February 19, 2016 15:18
-
-
Save Ninpo/59590457718fbc69cafd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
from __future__ import print_function | |
import time | |
import sys | |
import os | |
import boto | |
import gcs_oauth2_boto_plugin | |
from watchdog.observers import Observer | |
from watchdog.events import PatternMatchingEventHandler | |
class MyHandler(PatternMatchingEventHandler): | |
patterns = ["*.jpg", "*.pdf", "*.png"] | |
def process(self, event): | |
""" | |
event.event_type | |
'modified' | 'created' | 'moved' | 'deleted' | |
event.is_directory | |
True | False | |
event.src_path | |
path/to/observed/file | |
""" | |
# the file will be processed there | |
print("WatchDog:", event.src_path, event.event_type) | |
time.sleep(1) | |
#synctocloud(event.src_path) | |
def on_created(self, event): | |
self.process(event) | |
def on_modified(self, event): | |
self.process(event) | |
def on_deleted(self, event): | |
self.process(event) | |
def synctocloud(filepath): | |
googlebucket = "media1" | |
awsbucket = "media2" | |
googlestorage = "gs" | |
awsstorage = "s3" | |
project_id = dict(boto.config.items('GSUtil'))['default_project_id'] | |
header_values = {"x-goog-project-id": project_id} | |
cloudobject = os.sep.join(filepath.split(os.sep)[6:]) | |
with open(filepath, 'r') as localfile: | |
dst_uri = boto.storage_uri(googlebucket + '/' + cloudobject, googlestorage) | |
dst_uri.new_key().set_contents_from_file(localfile) | |
print("Successfully created %s/%s" % (dst_uri.bucket_name, dst_uri.object_name)) | |
localfile.seek(0) | |
dst_uri = boto.storage_uri(awsbucket + '/' + cloudobject, awsstorage) | |
dst_uri.new_key().set_contents_from_file(localfile) | |
print("Successfully created %s/%s" % (dst_uri.bucket_name, dst_uri.object_name)) | |
if __name__ == '__main__': | |
args = sys.argv[1:] | |
observer = Observer() | |
observer.schedule(MyHandler(), path=args[0] if args else '.', recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment