diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 7fed504904b5..37d6063f0abf 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -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 @@ -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') @@ -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 @@ -489,10 +488,9 @@ 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: @@ -500,10 +498,10 @@ def upload_from_filename(self, filename, content_type=None, 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:: @@ -525,10 +523,9 @@ 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') @@ -536,7 +533,7 @@ def upload_from_string(self, data, content_type='text/plain', 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. diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index a20cbe408d5c..6f7db2385a09 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -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. @@ -488,10 +488,9 @@ 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. @@ -499,10 +498,10 @@ def upload_file(self, filename, blob_name=None, connection=None): 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. @@ -535,10 +534,9 @@ 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. @@ -546,7 +544,7 @@ def upload_file_object(self, file_obj, blob_name=None, connection=None): 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 diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 26ca67a2ebb1..58d9ea6b8c1a 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -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, @@ -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 @@ -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') @@ -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 @@ -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') @@ -579,6 +581,7 @@ 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 @@ -586,7 +589,7 @@ def test_upload_from_file_w_slash_in_name(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) self.assertEqual(fh.tell(), len(DATA)) rq = connection.http._requested self.assertEqual(len(rq), 1) @@ -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) @@ -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') @@ -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') @@ -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') diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index 5bfd7e57d7bb..16ec568894f7 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -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): @@ -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): @@ -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): @@ -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):