Skip to content

Commit 4712d09

Browse files
committed
Falling back to implicit bucket in Blob constructor.
Also making bucket required to exit the constructor.
1 parent 38427df commit 4712d09

4 files changed

Lines changed: 106 additions & 79 deletions

File tree

gcloud/storage/blob.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,24 @@
2727

2828
from gcloud.storage._helpers import _PropertyMixin
2929
from gcloud.storage._helpers import _scalar_property
30+
from gcloud.storage import _implicit_environ
3031
from gcloud.storage.acl import ObjectACL
3132

3233

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

3649
CUSTOM_PROPERTY_ACCESSORS = {
3750
'acl': 'acl',
@@ -64,23 +77,18 @@ class Blob(_PropertyMixin):
6477
# ACL rules are lazily retrieved.
6578
_acl = None
6679

67-
def __init__(self, bucket=None, name=None, properties=None):
68-
"""Blob constructor.
69-
70-
:type bucket: :class:`gcloud.storage.bucket.Bucket`
71-
:param bucket: The bucket to which this blob belongs.
72-
73-
:type name: string
74-
:param name: The name of the blob. This corresponds to the
75-
unique path of the object in the bucket.
76-
77-
:type properties: dict
78-
:param properties: All the other data provided by Cloud Storage.
79-
"""
80+
def __init__(self, name, bucket=None, properties=None):
8081
if name is None and properties is not None:
8182
name = properties.get('name')
83+
8284
super(Blob, self).__init__(name=name, properties=properties)
85+
86+
if bucket is None:
87+
bucket = _implicit_environ.BUCKET
88+
8389
self.bucket = bucket
90+
if bucket is None:
91+
raise ValueError('A Blob must have a bucket set.')
8492

8593
@property
8694
def acl(self):
@@ -114,9 +122,7 @@ def path(self):
114122
:rtype: string
115123
:returns: The URL path to this Blob.
116124
"""
117-
if not self.bucket:
118-
raise ValueError('Cannot determine path without a bucket defined.')
119-
elif not self.name:
125+
if not self.name:
120126
raise ValueError('Cannot determine path without a blob name.')
121127

122128
return self.bucket.path + '/o/' + urllib.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):
@@ -158,7 +158,7 @@ def get_blob(self, blob):
158158
try:
159159
response = self.connection.api_request(method='GET',
160160
path=blob.path)
161-
return Blob(properties=response, bucket=self)
161+
return Blob(None, bucket=self, properties=response)
162162
except NotFound:
163163
return None
164164

0 commit comments

Comments
 (0)