s3_util.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from boto.s3.connection import OrdinaryCallingFormat, S3Connection
  2. from boto.exception import S3ResponseError
  3. import config
  4. import credentials
  5. __conn = None
  6. def get_connection():
  7. c = config.load_config()
  8. global __conn
  9. if not __conn:
  10. __conn = S3Connection(c["credentials"]["access_key"],
  11. c["credentials"]["secret_key"],
  12. calling_format=OrdinaryCallingFormat())
  13. return __conn
  14. def get_bucket_names(conn):
  15. l=[]
  16. for bucket in conn.get_all_buckets():
  17. l.append(bucket.name)
  18. return l
  19. def get_paths_from_keys(bucket):
  20. paths = {} #path -> key
  21. for key in bucket.get_all_keys():
  22. paths[key.name] = key
  23. return paths
  24. exc = None
  25. def test_bucket_exists(conn, bucket_name):
  26. try:
  27. bucket = conn.get_bucket(bucket_name)
  28. return True
  29. except S3ResponseError as e:
  30. if e.status == 404:
  31. return False
  32. elif e.status == 403:
  33. return True
  34. else:
  35. raise e