Skip to content

Commit

Permalink
Falling back to implicit bucket in Blob constructor.
Browse files Browse the repository at this point in the history
Also making bucket required to exit the constructor.
  • Loading branch information
dhermes committed Feb 5, 2015
1 parent 9518db7 commit 8506c76
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 80 deletions.
40 changes: 23 additions & 17 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@
from gcloud.credentials import generate_signed_url
from gcloud.storage._helpers import _PropertyMixin
from gcloud.storage._helpers import _scalar_property
from gcloud.storage import _implicit_environ
from gcloud.storage.acl import ObjectACL


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


class Blob(_PropertyMixin):
"""A wrapper around Cloud Storage's concept of an ``Object``."""
"""A wrapper around Cloud Storage's concept of an ``Object``.
:type name: string
:param name: The name of the blob. This corresponds to the
unique path of the object in the bucket.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket to which this blob belongs. Required, unless the
implicit default bucket has been set.
:type properties: dict
:param properties: All the other data provided by Cloud Storage.
"""

CUSTOM_PROPERTY_ACCESSORS = {
'acl': 'acl',
Expand Down Expand Up @@ -70,23 +83,18 @@ class Blob(_PropertyMixin):
# ACL rules are lazily retrieved.
_acl = None

def __init__(self, bucket=None, name=None, properties=None):
"""Blob constructor.
:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket to which this blob belongs.
:type name: string
:param name: The name of the blob. This corresponds to the
unique path of the object in the bucket.
:type properties: dict
:param properties: All the other data provided by Cloud Storage.
"""
def __init__(self, name, bucket=None, properties=None):
if name is None and properties is not None:
name = properties.get('name')

super(Blob, self).__init__(name=name, properties=properties)

if bucket is None:
bucket = _implicit_environ.BUCKET

self.bucket = bucket
if bucket is None:
raise ValueError('A Blob must have a bucket set.')

@property
def acl(self):
Expand Down Expand Up @@ -120,9 +128,7 @@ def path(self):
:rtype: string
:returns: The URL path to this Blob.
"""
if not self.bucket:
raise ValueError('Cannot determine path without a bucket defined.')
elif not self.name:
if not self.name:
raise ValueError('Cannot determine path without a blob name.')

return self.bucket.path + '/o/' + quote(self.name, safe='')
Expand Down
4 changes: 2 additions & 2 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_items_from_response(self, response):
"""
self.prefixes = tuple(response.get('prefixes', ()))
for item in response.get('items', []):
yield Blob(properties=item, bucket=self.bucket)
yield Blob(None, properties=item, bucket=self.bucket)


class Bucket(_PropertyMixin):
Expand Down Expand Up @@ -173,7 +173,7 @@ def get_blob(self, blob):
try:
response = self.connection.api_request(method='GET',
path=blob.path)
return Blob(properties=response, bucket=self)
return Blob(None, bucket=self, properties=response)
except NotFound:
return None

Expand Down
Loading

0 comments on commit 8506c76

Please sign in to comment.