Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ def exists(self):
:returns: True if the bucket exists in Cloud Storage.
"""
try:
self.connection.get_bucket(self.name)
# We only need the status code (200 or not) so we seek to
# minimize the returned payload.
query_params = {'fields': 'name'}
self.connection.api_request(method='GET', path=self.path,
query_params=query_params)
return True
except NotFound:
return False
Expand Down
38 changes: 27 additions & 11 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,30 +159,46 @@ class _FakeConnection(object):
_called_with = []

@classmethod
def get_bucket(cls, bucket_name):
cls._called_with.append(bucket_name)
raise NotFound(bucket_name)
def api_request(cls, *args, **kwargs):
cls._called_with.append((args, kwargs))
raise NotFound(args)

NAME = 'name'
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME)
self.assertFalse(bucket.exists())
self.assertEqual(_FakeConnection._called_with, [NAME])
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
'query_params': {
'fields': 'name',
},
}
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_exists_hit(self):
class _FakeConnection(object):

_called_with = []

@classmethod
def get_bucket(cls, bucket_name):
cls._called_with.append(bucket_name)
def api_request(cls, *args, **kwargs):
cls._called_with.append((args, kwargs))
# exists() does not use the return value
return object()

NAME = 'name'
bucket = self._makeOne(connection=_FakeConnection, name=NAME)
BUCKET_NAME = 'bucket-name'
bucket = self._makeOne(connection=_FakeConnection, name=BUCKET_NAME)
self.assertTrue(bucket.exists())
self.assertEqual(_FakeConnection._called_with, [NAME])
expected_called_kwargs = {
'method': 'GET',
'path': bucket.path,
'query_params': {
'fields': 'name',
},
}
expected_cw = [((), expected_called_kwargs)]
self.assertEqual(_FakeConnection._called_with, expected_cw)

def test_acl_property(self):
from gcloud.storage.acl import BucketACL
Expand Down