Skip to content

Commit

Permalink
Add keyword arguments to google.cloud.storage.Bucket.get_blob. (googl…
Browse files Browse the repository at this point in the history
  • Loading branch information
tillahoffmann authored and landrito committed Aug 21, 2017
1 parent ccc72be commit 7eb1a6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion storage/google/cloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Blob(_PropertyMixin):
:type encryption_key: bytes
:param encryption_key:
Optional 32 byte encryption key for customer-supplied encryption.
See https://cloud.google.com/storage/docs/encryption#customer-supplied
See https://cloud.google.com/storage/docs/encryption#customer-supplied.
"""

_chunk_size = None # Default value for each instance.
Expand Down
20 changes: 17 additions & 3 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from google.cloud.storage.acl import BucketACL
from google.cloud.storage.acl import DefaultObjectACL
from google.cloud.storage.blob import Blob
from google.cloud.storage.blob import _get_encryption_headers


def _blobs_page_start(iterator, page, response):
Expand Down Expand Up @@ -228,7 +229,7 @@ def path(self):

return self.path_helper(self.name)

def get_blob(self, blob_name, client=None):
def get_blob(self, blob_name, client=None, encryption_key=None, **kwargs):
"""Get a blob object by name.
This will return None if the blob doesn't exist:
Expand All @@ -245,14 +246,27 @@ def get_blob(self, blob_name, client=None):
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the current bucket.
:type encryption_key: bytes
:param encryption_key:
Optional 32 byte encryption key for customer-supplied encryption.
See
https://cloud.google.com/storage/docs/encryption#customer-supplied.
:type kwargs: dict
:param kwargs: Keyword arguments to pass to the
:class:`~google.cloud.storage.blob.Blob` constructor.
:rtype: :class:`google.cloud.storage.blob.Blob` or None
:returns: The blob object if it exists, otherwise None.
"""
client = self._require_client(client)
blob = Blob(bucket=self, name=blob_name)
blob = Blob(bucket=self, name=blob_name, encryption_key=encryption_key,
**kwargs)
try:
headers = _get_encryption_headers(encryption_key)
response = client._connection.api_request(
method='GET', path=blob.path, _target_object=blob)
method='GET', path=blob.path, _target_object=blob,
headers=headers)
# NOTE: We assume response.get('name') matches `blob_name`.
blob._set_properties(response)
# NOTE: This will not fail immediately in a batch. However, when
Expand Down
23 changes: 23 additions & 0 deletions storage/tests/unit/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ def test_get_blob_hit(self):
self.assertEqual(kw['method'], 'GET')
self.assertEqual(kw['path'], '/b/%s/o/%s' % (NAME, BLOB_NAME))

def test_get_blob_hit_with_kwargs(self):
from google.cloud.storage.blob import _get_encryption_headers

NAME = 'name'
BLOB_NAME = 'blob-name'
CHUNK_SIZE = 1024 * 1024
KEY = b'01234567890123456789012345678901' # 32 bytes

connection = _Connection({'name': BLOB_NAME})
client = _Client(connection)
bucket = self._make_one(name=NAME)
blob = bucket.get_blob(
BLOB_NAME, client=client, encryption_key=KEY, chunk_size=CHUNK_SIZE
)
self.assertIs(blob.bucket, bucket)
self.assertEqual(blob.name, BLOB_NAME)
kw, = connection._requested
self.assertEqual(kw['method'], 'GET')
self.assertEqual(kw['path'], '/b/%s/o/%s' % (NAME, BLOB_NAME))
self.assertEqual(kw['headers'], _get_encryption_headers(KEY))
self.assertEqual(blob.chunk_size, CHUNK_SIZE)
self.assertEqual(blob._encryption_key, KEY)

def test_list_blobs_defaults(self):
NAME = 'name'
connection = _Connection({'items': []})
Expand Down

0 comments on commit 7eb1a6a

Please sign in to comment.