bbb-s3.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os, glob, shutil, boto3, magic
  2. from botocore.exceptions import NoCredentialsError
  3. mime = magic.Magic(mime=True)
  4. ## Configuration Part
  5. BUCKET_NAME = 'bigbluebutton'
  6. DELETE_SERVER_FILES = True ## Set False (F should be capital) if you don't want to delete files from bbb-server
  7. def upload_to_aws(local_file, bucket, s3_file):
  8. ctype = mime.from_file(local_file)
  9. s3 = boto3.client('s3')
  10. try:
  11. s3.upload_file(local_file, bucket, s3_file, ExtraArgs={'ContentType': ctype, 'ACL': "public-read"})
  12. return True
  13. except OSError as e:
  14. if e.errno == errno.ENOENT:
  15. print("\nFile not found\n")
  16. except NoCredentialsError:
  17. return False
  18. def getListOfFiles(dirName):
  19. listOfFile = os.listdir(dirName)
  20. allFiles = list()
  21. for entry in listOfFile:
  22. fullPath = os.path.join(dirName, entry)
  23. if os.path.isdir(fullPath):
  24. allFiles = allFiles + getListOfFiles(fullPath)
  25. else:
  26. allFiles.append(fullPath)
  27. return allFiles
  28. def getListOfDirs(path):
  29. return [os.path.basename(x) for x in filter(
  30. os.path.isdir, glob.glob(os.path.join(path, '*')))]
  31. def remove(path):
  32. """ param <path> could either be relative or absolute. """
  33. if (os.path.isfile(path) or os.path.islink(path)) and "metadata.xml" not in path:
  34. os.remove(path) # remove the file
  35. elif os.path.isdir(path):
  36. shutil.rmtree(path) # remove dir and all contains
  37. else:
  38. pass
  39. #raise ValueError("file {} is not a file or dir.".format(path))
  40. def main():
  41. dirName = os.getcwd();
  42. listOfFiles = getListOfFiles(dirName)
  43. listOfDirs = getListOfDirs(dirName)
  44. for elem in listOfFiles:
  45. listOfFiles = list()
  46. for (dirpath, dirnames, filenames) in os.walk(dirName):
  47. listOfFiles += [os.path.join(dirpath, file) for file in filenames]
  48. for elem in listOfFiles:
  49. relative_path = elem.replace(dirName,'')[1:]
  50. if(relative_path!='bbb-s3.py'):
  51. print("\nUploading "+relative_path)
  52. uploaded = upload_to_aws(elem, BUCKET_NAME, relative_path)
  53. if(uploaded and DELETE_SERVER_FILES):
  54. remove(elem)
  55. else:
  56. print("\nNot able to delete files from your bbb-server, check file if its uploaded")
  57. file_length = len(listOfFiles)
  58. if(file_length==1):
  59. for folder in listOfDirs:
  60. remove(folder)
  61. if __name__ == '__main__':
  62. main()