You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB

4 years ago
  1. #!/home/alpcentaur/ProjektA/PrototypeWebApp/venv/bin/python3.5
  2. import boto
  3. from boto.exception import S3ResponseError
  4. from boto.s3.connection import OrdinaryCallingFormat
  5. def sizeof_fmt(num):
  6. for x in ['b ', 'KB', 'MB', 'GB', 'TB', 'XB']:
  7. if num < 1024.0:
  8. return "%3.1f %s" % (num, x)
  9. num /= 1024.0
  10. return "%3.1f %s" % (num, x)
  11. def list_bucket(b, prefix=None, marker=None):
  12. """List everything in a bucket"""
  13. from boto.s3.prefix import Prefix
  14. from boto.s3.key import Key
  15. total = 0
  16. if prefix:
  17. if not prefix.endswith("/"):
  18. prefix = prefix + "/"
  19. query = b.list(prefix=prefix, delimiter="/", marker=marker)
  20. print("%s" % prefix)
  21. else:
  22. query = b.list(delimiter="/", marker=marker)
  23. num = 0
  24. for k in query:
  25. num += 1
  26. mode = "-rwx---"
  27. if isinstance(k, Prefix):
  28. mode = "drwxr--"
  29. size = 0
  30. else:
  31. size = k.size
  32. for g in k.get_acl().acl.grants:
  33. if g.id == None:
  34. if g.permission == "READ":
  35. mode = "-rwxr--"
  36. elif g.permission == "FULL_CONTROL":
  37. mode = "-rwxrwx"
  38. if isinstance(k, Key):
  39. print("%s\t%s\t%010s\t%s" % (mode, k.last_modified,
  40. sizeof_fmt(size), k.name))
  41. else:
  42. #If it's not a Key object, it doesn't have a last_modified time, so
  43. #print nothing instead
  44. print("%s\t%s\t%010s\t%s" % (mode, ' ' * 24,
  45. sizeof_fmt(size), k.name))
  46. total += size
  47. print ("=" * 80)
  48. print ("\t\tTOTAL: \t%010s \t%i Files" % (sizeof_fmt(total), num))
  49. def list_buckets(s3, display_tags=False):
  50. """List all the buckets"""
  51. for b in s3.get_all_buckets():
  52. print(b.name)
  53. if display_tags:
  54. try:
  55. tags = b.get_tags()
  56. for tag in tags[0]:
  57. print(" %s:%s" % (tag.key, tag.value))
  58. except S3ResponseError as e:
  59. if e.status != 404:
  60. raise
  61. def main():
  62. import optparse
  63. import sys
  64. usage = "usage: %prog [options] [BUCKET1] [BUCKET2]"
  65. description = "List all S3 buckets OR list keys in the named buckets"
  66. parser = optparse.OptionParser(description=description, usage=usage)
  67. parser.add_option('-m', '--marker',
  68. help='The S3 key where the listing starts after it.')
  69. parser.add_option('-t', '--tags', action='store_true',
  70. help='Display tags when listing all buckets.')
  71. options, buckets = parser.parse_args()
  72. marker = options.marker
  73. if not buckets:
  74. list_buckets(boto.connect_s3(), options.tags)
  75. sys.exit(0)
  76. if options.tags:
  77. print("-t option only works for the overall bucket list")
  78. sys.exit(1)
  79. pairs = []
  80. mixedCase = False
  81. for name in buckets:
  82. if "/" in name:
  83. pairs.append(name.split("/", 1))
  84. else:
  85. pairs.append([name, None])
  86. if pairs[-1][0].lower() != pairs[-1][0]:
  87. mixedCase = True
  88. if mixedCase:
  89. s3 = boto.connect_s3(calling_format=OrdinaryCallingFormat())
  90. else:
  91. s3 = boto.connect_s3()
  92. for name, prefix in pairs:
  93. list_bucket(s3.get_bucket(name), prefix, marker=marker)
  94. if __name__ == "__main__":
  95. main()