spaces_3.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. def upload_dir(dirpath,bucket_name=None):
  2. client = set_client()
  3. if not os.path.isdir(dirpath):
  4. raise Exception("Not a Valid Directory :%s" % (dirpath))
  5. bucket = BUCKET_NAME
  6. if bucket_name:
  7. bucket = bucket_name
  8. file_count = 0
  9. error_count = 0
  10. print(color("Initializing ...\n",fg="yellow"))
  11. for (root, dirs, filenames) in os.walk(top=dirpath, topdown=True):
  12. for filename in filenames:
  13. filepath = os.path.join(root, filename)
  14. try:
  15. print(color("%s \nuploading...\n"%filepath,fg='blue'))
  16. client.upload_file(filepath, bucket ,os.path.abspath(filepath) ,Callback=ProgressPercentage(filepath))
  17. print(color("Success\n",fg="lime"))
  18. file_count += 1
  19. except Exception as e:
  20. logging.error('FilePath: %s' % filepath, exc_info=True)
  21. error_count += 1
  22. print(color("Error encountered: %s"%e,fg="#ff1a1a"))
  23. log_error("%s\n"%filepath)
  24. continue
  25. print(color("Total number of files uploaded: %d.\n" % file_count,fg="lime"))
  26. print(color("Total number of errors encountered: %d." % error_count,fg="#ff1a1a"))
  27. if error_count > 1:
  28. print(color("Please Check the log file located at .logs in the current working directory",fg='#ff1a1a'))