quick_echo_s3_py_3.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import sys
  2. from pprint import pprint
  3. from s3_helpers import *
  4. def main(args):
  5. expected_params = "Expected parametes: access_key [secret_key|decrypt:encypted_key] local_dir remote_bucket_and_prefix exclude cache_file [storage class] [access level acl] \r\n Or encrypt:secret_key to create an password encrypted secret key."
  6. if len(args) < 1:
  7. log(expected_params)
  8. exit(-1)
  9. enc_prefix = 'encrypt:'
  10. secret_key = args[0]
  11. if secret_key.startswith(enc_prefix):
  12. log('Password to encrypt AWS secret key:')
  13. password = getpass('')
  14. secret_key = encrypt_secret_key(password, secret_key[len(enc_prefix):])
  15. log('Key Encrypted as: ' + secret_key)
  16. log('Run again with decrypt:[encrypted secret key (above)] in place of the secret key.')
  17. exit(-1)
  18. if len(args) < 6:
  19. log(expected_params)
  20. exit(-1)
  21. else:
  22. access_key = args[0]
  23. secret_key = args[1]
  24. dec_prefix = 'decrypt:'
  25. if secret_key.startswith(dec_prefix):
  26. log('Password to access AWS:')
  27. password = getpass('')
  28. secret_key = decrypt_secret_key(password, secret_key[len(dec_prefix):])
  29. local_dir = args[2]
  30. remote = args[3]
  31. exclude = args[4]
  32. cache_file = args[5]
  33. storage_class = 'STANDARD'
  34. try:
  35. storage_class = args[6]
  36. except:
  37. pass
  38. access_level = 'private'
  39. try:
  40. access_level = args[7]
  41. except:
  42. pass
  43. remote_separator = remote.find("/")
  44. if remote_separator == -1:
  45. remote_bucket = remote
  46. remote_prefix = ""
  47. else:
  48. remote_bucket = remote[0:remote_separator]
  49. remote_prefix = remote[remote_separator+1:]
  50. if cache_file != "" and os.path.exists(cache_file):
  51. remote_map = load_remote_map(cache_file)
  52. else:
  53. remote_map = get_remote_file_map(access_key, secret_key, remote_bucket, remote_prefix)
  54. local_map = get_local_file_map(local_dir, exclude)
  55. to_upload, to_copy, to_delete = determine_operations(local_dir, local_map, remote_map)
  56. execute_operations(access_key, secret_key, local_dir, remote_bucket, remote_prefix,
  57. to_upload, to_copy, to_delete, remote_map, storage_class, access_level)
  58. if cache_file != "":
  59. save_remote_map(remote_map, cache_file)
  60. close_log_file()
  61. if __name__ == "__main__":
  62. main(sys.argv[1:])