Skip to content

Commit

Permalink
Accept and pass on a prefix value.
Browse files Browse the repository at this point in the history
  • Loading branch information
exarkun committed Aug 24, 2017
1 parent e64d2ec commit aa9f164
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion txaws/s3/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def delete_bucket(self, bucket):
query = self._query_factory(details)
return self._submit(query)

def get_bucket(self, bucket, marker=None, max_keys=None):
def get_bucket(self, bucket, marker=None, max_keys=None, prefix=None):
"""
Get a list of all the objects in a bucket.
Expand All @@ -205,6 +205,10 @@ def get_bucket(self, bucket, marker=None, max_keys=None):
return.
@type max_keys: L{int} or L{NoneType}
@param prefix: If given, indicate that only objects with keys
beginning with this value should be returned.
@type prefix: L{bytes} or L{NoneType}
@return: A L{Deferred} that fires with a L{BucketListing}
describing the result.
Expand All @@ -215,6 +219,8 @@ def get_bucket(self, bucket, marker=None, max_keys=None):
args.append(("marker", marker))
if max_keys is not None:
args.append(("max-keys", "%d" % (max_keys,)))
if prefix is not None:
args.append(("prefix", prefix))
if args:
object_name = "?" + urlencode(args)
else:
Expand Down
19 changes: 19 additions & 0 deletions txaws/s3/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,25 @@ def check_query_args(passthrough):
d.addCallback(check_query_args)
return d

def test_get_bucket_prefix(self):
"""
L{S3Client.get_bucket} accepts a C{prefix} argument to ask the server to
limit its response to objects beginning with a certain prefix.
"""
query_factory = mock_query_factory(payload.sample_get_bucket_result)
def check_query_args(passthrough):
self.assertEqual(
b"http:///mybucket/?prefix=foobar",
query_factory.details.url_context.get_encoded_url(),
)
return passthrough

creds = AWSCredentials("foo", "bar")
s3 = client.S3Client(creds, query_factory=query_factory)
d = s3.get_bucket("mybucket", prefix=b"foobar")
d.addCallback(check_query_args)
return d

def test_get_bucket_location(self):
"""
L{S3Client.get_bucket_location} creates a L{Query} to get a bucket's
Expand Down

0 comments on commit aa9f164

Please sign in to comment.