From 7903f61b44ef9515a0a191dc907d46eb9f571194 Mon Sep 17 00:00:00 2001 From: Giulio Date: Wed, 10 Mar 2021 21:45:00 +0100 Subject: [PATCH] Import --- Readme.md | 7 +++++ bbb-s3.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 Readme.md create mode 100644 bbb-s3.py diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..62a55ee --- /dev/null +++ b/Readme.md @@ -0,0 +1,7 @@ +Forked froma script that was once available on GitHub and now deleted. + +Configure Boto3 with the Object Storage endpoint and credentials. + +Change the endpoint in the Javascript file that play the presentation and anywhhere else in BBB templates. + +This script should run in cron and it is possible to choose if the original files have to be deleted or not. `metadata.xml` is always kept locally bevcause it's what BBB uses to keep track of the records. diff --git a/bbb-s3.py b/bbb-s3.py new file mode 100644 index 0000000..17d1c57 --- /dev/null +++ b/bbb-s3.py @@ -0,0 +1,77 @@ +import os, glob, shutil, boto3, magic +from botocore.exceptions import NoCredentialsError +mime = magic.Magic(mime=True) +## Configuration Part +BUCKET_NAME = 'bigbluebutton' +DELETE_SERVER_FILES = True ## Set False (F should be capital) if you don't want to delete files from bbb-server + +def upload_to_aws(local_file, bucket, s3_file): + ctype = mime.from_file(local_file) + s3 = boto3.client('s3') + + try: + s3.upload_file(local_file, bucket, s3_file, ExtraArgs={'ContentType': ctype, 'ACL': "public-read"}) + return True + except OSError as e: + if e.errno == errno.ENOENT: + print("\nFile not found\n") + except NoCredentialsError: + return False + +def getListOfFiles(dirName): + listOfFile = os.listdir(dirName) + allFiles = list() + for entry in listOfFile: + fullPath = os.path.join(dirName, entry) + if os.path.isdir(fullPath): + allFiles = allFiles + getListOfFiles(fullPath) + else: + allFiles.append(fullPath) + + return allFiles + +def getListOfDirs(path): + return [os.path.basename(x) for x in filter( + os.path.isdir, glob.glob(os.path.join(path, '*')))] + +def remove(path): + """ param could either be relative or absolute. """ + if (os.path.isfile(path) or os.path.islink(path)) and "metadata.xml" not in path: + os.remove(path) # remove the file + elif os.path.isdir(path): + shutil.rmtree(path) # remove dir and all contains + else: + pass + #raise ValueError("file {} is not a file or dir.".format(path)) + +def main(): + + dirName = os.getcwd(); + listOfFiles = getListOfFiles(dirName) + listOfDirs = getListOfDirs(dirName) + for elem in listOfFiles: + listOfFiles = list() + for (dirpath, dirnames, filenames) in os.walk(dirName): + listOfFiles += [os.path.join(dirpath, file) for file in filenames] + + for elem in listOfFiles: + relative_path = elem.replace(dirName,'')[1:] + if(relative_path!='bbb-s3.py'): + print("\nUploading "+relative_path) + uploaded = upload_to_aws(elem, BUCKET_NAME, relative_path) + if(uploaded and DELETE_SERVER_FILES): + remove(elem) + else: + print("\nNot able to delete files from your bbb-server, check file if its uploaded") + + file_length = len(listOfFiles) + if(file_length==1): + for folder in listOfDirs: + remove(folder) + + + + + +if __name__ == '__main__': + main()