Skip to content

Commit 2d40ccd

Browse files
committed
Falling back to implicit bucket in Blob constructor.
Also making bucket required to exit the constructor.
1 parent 9518db7 commit 2d40ccd

File tree

4 files changed

+107
-79
lines changed

4 files changed

+107
-79
lines changed

gcloud/storage/blob.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,27 @@
3030
from gcloud.credentials import generate_signed_url
3131
from gcloud.storage._helpers import _PropertyMixin
3232
from gcloud.storage._helpers import _scalar_property
33+
from gcloud.storage import _implicit_environ
3334
from gcloud.storage.acl import ObjectACL
3435

3536

3637
_API_ACCESS_ENDPOINT = 'https://storage.googleapis.com'
3738

3839

3940
class Blob(_PropertyMixin):
40-
"""A wrapper around Cloud Storage's concept of an ``Object``."""
41+
"""A wrapper around Cloud Storage's concept of an ``Object``.
42+
43+
:type name: string
44+
:param name: The name of the blob. This corresponds to the
45+
unique path of the object in the bucket.
46+
47+
:type bucket: :class:`gcloud.storage.bucket.Bucket`
48+
:param bucket: The bucket to which this blob belongs. Required, unless the
49+
implicit default bucket has been set.
50+
51+
:type properties: dict
52+
:param properties: All the other data provided by Cloud Storage.
53+
"""
4154

4255
CUSTOM_PROPERTY_ACCESSORS = {
4356
'acl': 'acl',
@@ -70,22 +83,18 @@ class Blob(_PropertyMixin):
7083
# ACL rules are lazily retrieved.
7184
_acl = None
7285

73-
def __init__(self, bucket=None, name=None, properties=None):
74-
"""Blob constructor.
86+
def __init__(self, name, bucket=None, properties=None):
87+
if name is None and properties is not None:
88+
name = properties.get('name')
7589

76-
:type bucket: :class:`gcloud.storage.bucket.Bucket`
77-
:param bucket: The bucket to which this blob belongs.
90+
if bucket is None:
91+
bucket = _implicit_environ.BUCKET
7892

79-
:type name: string
80-
:param name: The name of the blob. This corresponds to the
81-
unique path of the object in the bucket.
93+
if bucket is None:
94+
raise ValueError('A Blob must have a bucket set.')
8295

83-
:type properties: dict
84-
:param properties: All the other data provided by Cloud Storage.
85-
"""
86-
if name is None and properties is not None:
87-
name = properties.get('name')
8896
super(Blob, self).__init__(name=name, properties=properties)
97+
8998
self.bucket = bucket
9099

91100
@property
@@ -120,9 +129,7 @@ def path(self):
120129
:rtype: string
121130
:returns: The URL path to this Blob.
122131
"""
123-
if not self.bucket:
124-
raise ValueError('Cannot determine path without a bucket defined.')
125-
elif not self.name:
132+
if not self.name:
126133
raise ValueError('Cannot determine path without a blob name.')
127134

128135
return self.bucket.path + '/o/' + quote(self.name, safe='')

gcloud/storage/bucket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def get_items_from_response(self, response):
5050
"""
5151
self.prefixes = tuple(response.get('prefixes', ()))
5252
for item in response.get('items', []):
53-
yield Blob(properties=item, bucket=self.bucket)
53+
yield Blob(None, properties=item, bucket=self.bucket)
5454

5555

5656
class Bucket(_PropertyMixin):
@@ -173,7 +173,7 @@ def get_blob(self, blob):
173173
try:
174174
response = self.connection.api_request(method='GET',
175175
path=blob.path)
176-
return Blob(properties=response, bucket=self)
176+
return Blob(None, bucket=self, properties=response)
177177
except NotFound:
178178
return None
179179

0 commit comments

Comments
 (0)