diff --git a/gcloud/storage/blob.py b/gcloud/storage/blob.py index 2926ec2992f6..ed6a00dda966 100644 --- a/gcloud/storage/blob.py +++ b/gcloud/storage/blob.py @@ -232,7 +232,7 @@ def exists(self, connection=None): except NotFound: return False - def rename(self, new_name): + def rename(self, new_name, connection=None): """Renames this blob using copy and delete operations. Effectively, copies blob to the same bucket with a new name, then @@ -246,23 +246,34 @@ def rename(self, new_name): :type new_name: string :param new_name: The new name for this blob. + :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. + :rtype: :class:`Blob` :returns: The newly-copied blob. """ - new_blob = self.bucket.copy_blob(self, self.bucket, new_name) - self.delete() + new_blob = self.bucket.copy_blob(self, self.bucket, new_name, + connection=connection) + self.delete(connection=connection) return new_blob - def delete(self): + def delete(self, connection=None): """Deletes a blob from Cloud Storage. + :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. + :rtype: :class:`Blob` :returns: The blob that was just deleted. :raises: :class:`gcloud.exceptions.NotFound` (propagated from :meth:`gcloud.storage.bucket.Bucket.delete_blob`). """ - return self.bucket.delete_blob(self.name) + return self.bucket.delete_blob(self.name, connection=connection) def download_to_file(self, file_obj, connection=None): """Download the contents of this blob into a file-like object. diff --git a/gcloud/storage/test_blob.py b/gcloud/storage/test_blob.py index 07791862ba53..7758b5153dc9 100644 --- a/gcloud/storage/test_blob.py +++ b/gcloud/storage/test_blob.py @@ -291,7 +291,21 @@ def test_rename(self): self.assertEqual(blob.name, BLOB_NAME) self.assertEqual(new_blob.name, NEW_NAME) self.assertFalse(BLOB_NAME in bucket._blobs) - self.assertTrue(BLOB_NAME in bucket._deleted) + self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) + self.assertTrue(NEW_NAME in bucket._blobs) + + def test_rename_w_explicit_connection(self): + BLOB_NAME = 'blob-name' + NEW_NAME = 'new-name' + connection = _Connection() + bucket = _Bucket(None) + blob = self._makeOne(BLOB_NAME, bucket=bucket) + bucket._blobs[BLOB_NAME] = 1 + new_blob = blob.rename(NEW_NAME, connection=connection) + self.assertEqual(blob.name, BLOB_NAME) + self.assertEqual(new_blob.name, NEW_NAME) + self.assertFalse(BLOB_NAME in bucket._blobs) + self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) self.assertTrue(NEW_NAME in bucket._blobs) def test_delete(self): @@ -299,11 +313,24 @@ def test_delete(self): BLOB_NAME = 'blob-name' not_found_response = {'status': NOT_FOUND} connection = _Connection(not_found_response) - bucket = _Bucket(None) + bucket = _Bucket(connection) blob = self._makeOne(BLOB_NAME, bucket=bucket) bucket._blobs[BLOB_NAME] = 1 blob.delete() self.assertFalse(blob.exists(connection=connection)) + self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) + + def test_delete_w_explicit_connection(self): + from six.moves.http_client import NOT_FOUND + BLOB_NAME = 'blob-name' + not_found_response = {'status': NOT_FOUND} + connection = _Connection(not_found_response) + bucket = _Bucket(None) + blob = self._makeOne(BLOB_NAME, bucket=bucket) + bucket._blobs[BLOB_NAME] = 1 + blob.delete(connection=connection) + self.assertFalse(blob.exists(connection=connection)) + self.assertEqual(bucket._deleted, [(BLOB_NAME, connection)]) def _download_to_file_helper(self, chunk_size=None): from six.moves.http_client import OK @@ -1111,15 +1138,21 @@ class _Bucket(object): def __init__(self, connection): self.connection = connection self._blobs = {} + self._copied = [] self._deleted = [] - def copy_blob(self, blob, destination_bucket, new_name): + def copy_blob(self, blob, destination_bucket, new_name, connection=None): + if connection is None: + connection = self.connection + self._copied.append((blob, destination_bucket, new_name, connection)) destination_bucket._blobs[new_name] = self._blobs[blob.name] return blob.__class__(new_name, bucket=destination_bucket) - def delete_blob(self, blob_name): + def delete_blob(self, blob_name, connection=None): + if connection is None: + connection = self.connection del self._blobs[blob_name] - self._deleted.append(blob_name) + self._deleted.append((blob_name, connection)) class _Signer(object):