Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Falling back to implicit bucket in Blob constructor. #585

Merged
merged 1 commit into from
Feb 6, 2015
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
39 changes: 23 additions & 16 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,22 +83,18 @@ class Blob(_PropertyMixin):
# ACL rules are lazily retrieved.
_acl = None

def __init__(self, bucket=None, name=None, properties=None):
"""Blob constructor.
def __init__(self, name, bucket=None, properties=None):
if name is None and properties is not None:
name = properties.get('name')

:type bucket: :class:`gcloud.storage.bucket.Bucket`
:param bucket: The bucket to which this blob belongs.
if bucket is None:
bucket = _implicit_environ.BUCKET

:type name: string
:param name: The name of the blob. This corresponds to the
unique path of the object in the bucket.
if bucket is None:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

raise ValueError('A Blob must have a bucket set.')

:type properties: dict
:param properties: All the other data provided by Cloud Storage.
"""
if name is None and properties is not None:
name = properties.get('name')
super(Blob, self).__init__(name=name, properties=properties)

self.bucket = bucket

@property
Expand Down Expand Up @@ -120,9 +129,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)

This comment was marked as spam.

This comment was marked as spam.



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