Skip to content

Commit

Permalink
Updating Blob.upload* and Bucket.upload* methods to accept a client.
Browse files Browse the repository at this point in the history
Towards #952, removing connection from methods / constructors.
  • Loading branch information
dhermes committed Jul 13, 2015
1 parent 301bc1c commit 83590a1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
33 changes: 15 additions & 18 deletions gcloud/storage/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def download_as_string(self, client=None):
return string_buffer.getvalue()

def upload_from_file(self, file_obj, rewind=False, size=None,
content_type=None, num_retries=6, connection=None):
content_type=None, num_retries=6, client=None):
"""Upload the contents of this blob from a file-like object.
The content type of the upload will either be
Expand Down Expand Up @@ -393,15 +393,14 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
:type num_retries: integer
:param num_retries: Number of upload retries. Defaults to 6.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:raises: :class:`ValueError` if size is not passed in and can not be
determined
"""
connection = _require_connection(connection)
connection = self._client_or_connection(client)
content_type = (content_type or self._properties.get('contentType') or
'application/octet-stream')

Expand Down Expand Up @@ -464,7 +463,7 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
self._set_properties(json.loads(response_content))

def upload_from_filename(self, filename, content_type=None,
connection=None):
client=None):
"""Upload this blob's contents from the content of a named file.
The content type of the upload will either be
Expand All @@ -489,21 +488,20 @@ def upload_from_filename(self, filename, content_type=None,
:type content_type: string or ``NoneType``
:param content_type: Optional type of content being uploaded.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
content_type = content_type or self._properties.get('contentType')
if content_type is None:
content_type, _ = mimetypes.guess_type(filename)

with open(filename, 'rb') as file_obj:
self.upload_from_file(file_obj, content_type=content_type,
connection=connection)
client=client)

def upload_from_string(self, data, content_type='text/plain',
connection=None):
client=None):
"""Upload contents of this blob from the provided string.
.. note::
Expand All @@ -525,18 +523,17 @@ def upload_from_string(self, data, content_type='text/plain',
:param content_type: Optional type of content being uploaded. Defaults
to ``'text/plain'``.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
"""
if isinstance(data, six.text_type):
data = data.encode('utf-8')
string_buffer = BytesIO()
string_buffer.write(data)
self.upload_from_file(file_obj=string_buffer, rewind=True,
size=len(data), content_type=content_type,
connection=connection)
client=client)

def make_public(self, connection=None):
"""Make this blob public giving all users read access.
Expand Down
22 changes: 10 additions & 12 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def copy_blob(blob, destination_bucket, new_name=None,
new_blob._set_properties(copy_result)
return new_blob

def upload_file(self, filename, blob_name=None, connection=None):
def upload_file(self, filename, blob_name=None, client=None):
"""Shortcut method to upload a file into this bucket.
Use this method to quickly put a local file in Cloud Storage.
Expand Down Expand Up @@ -488,21 +488,20 @@ def upload_file(self, filename, blob_name=None, connection=None):
of the bucket with the same name as on your local
file system.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`Blob`
:returns: The updated Blob object.
"""
if blob_name is None:
blob_name = os.path.basename(filename)
blob = Blob(bucket=self, name=blob_name)
blob.upload_from_filename(filename, connection=connection)
blob.upload_from_filename(filename, client=client)
return blob

def upload_file_object(self, file_obj, blob_name=None, connection=None):
def upload_file_object(self, file_obj, blob_name=None, client=None):
"""Shortcut method to upload a file object into this bucket.
Use this method to quickly put a local file in Cloud Storage.
Expand Down Expand Up @@ -535,18 +534,17 @@ def upload_file_object(self, file_obj, blob_name=None, connection=None):
of the bucket with the same name as on your local
file system.
:type connection: :class:`gcloud.storage.connection.Connection` or
``NoneType``
:param connection: Optional. The connection to use when sending
requests. If not provided, falls back to default.
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to default connection.
:rtype: :class:`Blob`
:returns: The updated Blob object.
"""
if blob_name is None:
blob_name = os.path.basename(file_obj.name)
blob = Blob(bucket=self, name=blob_name)
blob.upload_from_file(file_obj, connection=connection)
blob.upload_from_file(file_obj, client=client)
return blob

@property
Expand Down
22 changes: 14 additions & 8 deletions gcloud/storage/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ def test_upload_from_file_size_failure(self):
blob = self._makeOne(BLOB_NAME, bucket=bucket)
file_obj = object()
connection = _Connection()
client = _Client(connection)
with self.assertRaises(ValueError):
blob.upload_from_file(file_obj, size=None,
connection=connection)
blob.upload_from_file(file_obj, size=None, client=client)

def _upload_from_file_simple_test_helper(self, properties=None,
content_type_arg=None,
Expand All @@ -451,6 +451,7 @@ def _upload_from_file_simple_test_helper(self, properties=None,
connection = _Connection(
(response, b'{}'),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket, properties=properties)
blob._CHUNK_SIZE_MULTIPLE = 1
Expand All @@ -460,7 +461,7 @@ def _upload_from_file_simple_test_helper(self, properties=None,
fh.flush()
blob.upload_from_file(fh, rewind=True,
content_type=content_type_arg,
connection=connection)
client=client)
rq = connection.http._requested
self.assertEqual(len(rq), 1)
self.assertEqual(rq[0]['method'], 'POST')
Expand Down Expand Up @@ -521,6 +522,7 @@ def test_upload_from_file_resumable(self):
(chunk1_response, b''),
(chunk2_response, b'{}'),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
blob._CHUNK_SIZE_MULTIPLE = 1
Expand All @@ -530,7 +532,7 @@ def test_upload_from_file_resumable(self):
with NamedTemporaryFile() as fh:
fh.write(DATA)
fh.flush()
blob.upload_from_file(fh, rewind=True, connection=connection)
blob.upload_from_file(fh, rewind=True, client=client)
rq = connection.http._requested
self.assertEqual(len(rq), 3)
self.assertEqual(rq[0]['method'], 'POST')
Expand Down Expand Up @@ -579,14 +581,15 @@ def test_upload_from_file_w_slash_in_name(self):
(chunk1_response, ''),
(chunk2_response, ''),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = 5
with NamedTemporaryFile() as fh:
fh.write(DATA)
fh.flush()
blob.upload_from_file(fh, rewind=True, connection=connection)
blob.upload_from_file(fh, rewind=True, client=client)
self.assertEqual(fh.tell(), len(DATA))
rq = connection.http._requested
self.assertEqual(len(rq), 1)
Expand Down Expand Up @@ -626,6 +629,7 @@ def _upload_from_filename_test_helper(self, properties=None,
(chunk1_response, ''),
(chunk2_response, ''),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket,
properties=properties)
Expand All @@ -635,7 +639,7 @@ def _upload_from_filename_test_helper(self, properties=None,
fh.write(DATA)
fh.flush()
blob.upload_from_filename(fh.name, content_type=content_type_arg,
connection=connection)
client=client)
rq = connection.http._requested
self.assertEqual(len(rq), 1)
self.assertEqual(rq[0]['method'], 'POST')
Expand Down Expand Up @@ -692,11 +696,12 @@ def test_upload_from_string_w_bytes(self):
(chunk1_response, ''),
(chunk2_response, ''),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = 5
blob.upload_from_string(DATA, connection=connection)
blob.upload_from_string(DATA, client=client)
rq = connection.http._requested
self.assertEqual(len(rq), 1)
self.assertEqual(rq[0]['method'], 'POST')
Expand Down Expand Up @@ -731,11 +736,12 @@ def test_upload_from_string_w_text(self):
(chunk1_response, ''),
(chunk2_response, ''),
)
client = _Client(connection)
bucket = _Bucket()
blob = self._makeOne(BLOB_NAME, bucket=bucket)
blob._CHUNK_SIZE_MULTIPLE = 1
blob.chunk_size = 5
blob.upload_from_string(DATA, connection=connection)
blob.upload_from_string(DATA, client=client)
rq = connection.http._requested
self.assertEqual(len(rq), 1)
self.assertEqual(rq[0]['method'], 'POST')
Expand Down
16 changes: 8 additions & 8 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,9 +544,9 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def upload_from_filename(self, filename, connection=None):
def upload_from_filename(self, filename, client=None):
_uploaded.append((self._bucket, self._name, filename,
connection))
client))

bucket = self._makeOne()
with _Monkey(MUT, Blob=_Blob):
Expand All @@ -566,9 +566,9 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def upload_from_filename(self, filename, connection=None):
def upload_from_filename(self, filename, client=None):
_uploaded.append((self._bucket, self._name, filename,
connection))
client))

bucket = self._makeOne()
with _Monkey(MUT, Blob=_Blob):
Expand All @@ -588,8 +588,8 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def upload_from_file(self, fh, connection=None):
_uploaded.append((self._bucket, self._name, fh, connection))
def upload_from_file(self, fh, client=None):
_uploaded.append((self._bucket, self._name, fh, client))

bucket = self._makeOne()
with _Monkey(MUT, Blob=_Blob):
Expand All @@ -613,8 +613,8 @@ def __init__(self, bucket, name):
self._bucket = bucket
self._name = name

def upload_from_file(self, fh, connection=None):
_uploaded.append((self._bucket, self._name, fh, connection))
def upload_from_file(self, fh, client=None):
_uploaded.append((self._bucket, self._name, fh, client))

bucket = self._makeOne()
with _Monkey(MUT, Blob=_Blob):
Expand Down

1 comment on commit 83590a1

@tseaver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit LGTM

Please sign in to comment.