PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/boto-2.5.2/boto/s3/bucketlistresultset.py

#
Python | 141 lines | 120 code | 1 blank | 20 comment | 0 complexity | de512761ae287c43955f5c6c2b47851d MD5 | raw file
  1. # Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a
  4. # copy of this software and associated documentation files (the
  5. # "Software"), to deal in the Software without restriction, including
  6. # without limitation the rights to use, copy, modify, merge, publish, dis-
  7. # tribute, sublicense, and/or sell copies of the Software, and to permit
  8. # persons to whom the Software is furnished to do so, subject to the fol-
  9. # lowing conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included
  12. # in all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  16. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  17. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. # IN THE SOFTWARE.
  21. def bucket_lister(bucket, prefix='', delimiter='', marker='', headers=None):
  22. """
  23. A generator function for listing keys in a bucket.
  24. """
  25. more_results = True
  26. k = None
  27. while more_results:
  28. rs = bucket.get_all_keys(prefix=prefix, marker=marker,
  29. delimiter=delimiter, headers=headers)
  30. for k in rs:
  31. yield k
  32. if k:
  33. marker = rs.next_marker or k.name
  34. more_results= rs.is_truncated
  35. class BucketListResultSet:
  36. """
  37. A resultset for listing keys within a bucket. Uses the bucket_lister
  38. generator function and implements the iterator interface. This
  39. transparently handles the results paging from S3 so even if you have
  40. many thousands of keys within the bucket you can iterate over all
  41. keys in a reasonably efficient manner.
  42. """
  43. def __init__(self, bucket=None, prefix='', delimiter='', marker='', headers=None):
  44. self.bucket = bucket
  45. self.prefix = prefix
  46. self.delimiter = delimiter
  47. self.marker = marker
  48. self.headers = headers
  49. def __iter__(self):
  50. return bucket_lister(self.bucket, prefix=self.prefix,
  51. delimiter=self.delimiter, marker=self.marker,
  52. headers=self.headers)
  53. def versioned_bucket_lister(bucket, prefix='', delimiter='',
  54. key_marker='', version_id_marker='', headers=None):
  55. """
  56. A generator function for listing versions in a bucket.
  57. """
  58. more_results = True
  59. k = None
  60. while more_results:
  61. rs = bucket.get_all_versions(prefix=prefix, key_marker=key_marker,
  62. version_id_marker=version_id_marker,
  63. delimiter=delimiter, headers=headers,
  64. max_keys=999)
  65. for k in rs:
  66. yield k
  67. key_marker = rs.next_key_marker
  68. version_id_marker = rs.next_version_id_marker
  69. more_results= rs.is_truncated
  70. class VersionedBucketListResultSet:
  71. """
  72. A resultset for listing versions within a bucket. Uses the bucket_lister
  73. generator function and implements the iterator interface. This
  74. transparently handles the results paging from S3 so even if you have
  75. many thousands of keys within the bucket you can iterate over all
  76. keys in a reasonably efficient manner.
  77. """
  78. def __init__(self, bucket=None, prefix='', delimiter='', key_marker='',
  79. version_id_marker='', headers=None):
  80. self.bucket = bucket
  81. self.prefix = prefix
  82. self.delimiter = delimiter
  83. self.key_marker = key_marker
  84. self.version_id_marker = version_id_marker
  85. self.headers = headers
  86. def __iter__(self):
  87. return versioned_bucket_lister(self.bucket, prefix=self.prefix,
  88. delimiter=self.delimiter,
  89. key_marker=self.key_marker,
  90. version_id_marker=self.version_id_marker,
  91. headers=self.headers)
  92. def multipart_upload_lister(bucket, key_marker='',
  93. upload_id_marker='',
  94. headers=None):
  95. """
  96. A generator function for listing multipart uploads in a bucket.
  97. """
  98. more_results = True
  99. k = None
  100. while more_results:
  101. rs = bucket.get_all_multipart_uploads(key_marker=key_marker,
  102. upload_id_marker=upload_id_marker,
  103. headers=headers)
  104. for k in rs:
  105. yield k
  106. key_marker = rs.next_key_marker
  107. upload_id_marker = rs.next_upload_id_marker
  108. more_results= rs.is_truncated
  109. class MultiPartUploadListResultSet:
  110. """
  111. A resultset for listing multipart uploads within a bucket.
  112. Uses the multipart_upload_lister generator function and
  113. implements the iterator interface. This
  114. transparently handles the results paging from S3 so even if you have
  115. many thousands of uploads within the bucket you can iterate over all
  116. keys in a reasonably efficient manner.
  117. """
  118. def __init__(self, bucket=None, key_marker='',
  119. upload_id_marker='', headers=None):
  120. self.bucket = bucket
  121. self.key_marker = key_marker
  122. self.upload_id_marker = upload_id_marker
  123. self.headers = headers
  124. def __iter__(self):
  125. return multipart_upload_lister(self.bucket,
  126. key_marker=self.key_marker,
  127. upload_id_marker=self.upload_id_marker,
  128. headers=self.headers)