Skip to content

Commit 8506c76

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

File tree

4 files changed

+107
-80
lines changed

4 files changed

+107
-80
lines changed

gcloud/storage/blob.py

Lines changed: 23 additions & 17 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,23 +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.
75-
76-
:type bucket: :class:`gcloud.storage.bucket.Bucket`
77-
:param bucket: The bucket to which this blob belongs.
78-
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.
82-
83-
:type properties: dict
84-
:param properties: All the other data provided by Cloud Storage.
85-
"""
86+
def __init__(self, name, bucket=None, properties=None):
8687
if name is None and properties is not None:
8788
name = properties.get('name')
89+
8890
super(Blob, self).__init__(name=name, properties=properties)
91+
92+
if bucket is None:
93+
bucket = _implicit_environ.BUCKET
94+
8995
self.bucket = bucket
96+
if bucket is None:
97+
raise ValueError('A Blob must have a bucket set.')
9098

9199
@property
92100
def acl(self):
@@ -120,9 +128,7 @@ def path(self):
120128
:rtype: string
121129
:returns: The URL path to this Blob.
122130
"""
123-
if not self.bucket:
124-
raise ValueError('Cannot determine path without a bucket defined.')
125-
elif not self.name:
131+
if not self.name:
126132
raise ValueError('Cannot determine path without a blob name.')
127133

128134
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)