diff --git a/gcloud/storage/bucket.py b/gcloud/storage/bucket.py index 1647a8489bdd..efdb7b567c7f 100644 --- a/gcloud/storage/bucket.py +++ b/gcloud/storage/bucket.py @@ -428,7 +428,9 @@ def delete_blobs(self, blobs, on_error=None, connection=None): else: raise - def copy_blob(self, blob, destination_bucket, new_name=None): + @staticmethod + def copy_blob(blob, destination_bucket, new_name=None, + connection=None): """Copy the given blob to the given bucket, optionally with a new name. :type blob: string or :class:`gcloud.storage.blob.Blob` @@ -441,18 +443,24 @@ def copy_blob(self, blob, destination_bucket, new_name=None): :type new_name: string :param new_name: (optional) the new name for the copied file. + :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:`gcloud.storage.blob.Blob` :returns: The new Blob. """ + connection = _require_connection(connection) if new_name is None: new_name = blob.name new_blob = Blob(bucket=destination_bucket, name=new_name) api_path = blob.path + '/copyTo' + new_blob.path - copy_result = self.connection.api_request(method='POST', path=api_path) + copy_result = connection.api_request(method='POST', path=api_path) new_blob._set_properties(copy_result) return new_blob - def upload_file(self, filename, blob_name=None): + def upload_file(self, filename, blob_name=None, connection=None): """Shortcut method to upload a file into this bucket. Use this method to quickly put a local file in Cloud Storage. @@ -485,16 +493,21 @@ def upload_file(self, filename, blob_name=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. + :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) + blob.upload_from_filename(filename, connection=connection) return blob - def upload_file_object(self, file_obj, blob_name=None): + def upload_file_object(self, file_obj, blob_name=None, connection=None): """Shortcut method to upload a file object into this bucket. Use this method to quickly put a local file in Cloud Storage. @@ -527,13 +540,18 @@ def upload_file_object(self, file_obj, blob_name=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. + :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) + blob.upload_from_file(file_obj, connection=connection) return blob @property diff --git a/gcloud/storage/test_bucket.py b/gcloud/storage/test_bucket.py index c7d58f7b1f5d..4f31859c7970 100644 --- a/gcloud/storage/test_bucket.py +++ b/gcloud/storage/test_bucket.py @@ -194,8 +194,10 @@ def test_create_no_project(self): from gcloud._testing import _monkey_defaults BUCKET_NAME = 'bucket-name' bucket = self._makeOne(BUCKET_NAME) + CONNECTION = object() with _monkey_defaults(project=None): - self.assertRaises(EnvironmentError, bucket.create) + self.assertRaises(EnvironmentError, bucket.create, + connection=CONNECTION) def test_create_hit_explicit_project(self): BUCKET_NAME = 'bucket-name' @@ -391,7 +393,8 @@ def test_delete_explicit_too_many(self): # Make the Bucket refuse to delete with 2 objects. bucket._MAX_OBJECTS_FOR_BUCKET_DELETE = 1 - self.assertRaises(ValueError, bucket.delete, force=True) + self.assertRaises(ValueError, bucket.delete, force=True, + connection=connection) self.assertEqual(connection._deleted_buckets, []) def test_delete_blob_miss(self): @@ -420,8 +423,8 @@ def test_delete_blob_hit(self): def test_delete_blobs_empty(self): NAME = 'name' connection = _Connection() - bucket = self._makeOne(NAME, connection) - bucket.delete_blobs([]) + bucket = self._makeOne(NAME) + bucket.delete_blobs([], connection=connection) self.assertEqual(connection._requested, []) def test_delete_blobs_hit(self): @@ -478,10 +481,10 @@ class _Blob(object): path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME) connection = _Connection({}) - source = self._makeOne(SOURCE, connection) - dest = self._makeOne(DEST, connection) + source = self._makeOne(SOURCE) + dest = self._makeOne(DEST) blob = _Blob() - new_blob = source.copy_blob(blob, dest) + new_blob = source.copy_blob(blob, dest, connection=connection) self.assertTrue(new_blob.bucket is dest) self.assertEqual(new_blob.name, BLOB_NAME) kw, = connection._requested @@ -501,10 +504,11 @@ class _Blob(object): path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME) connection = _Connection({}) - source = self._makeOne(SOURCE, connection) - dest = self._makeOne(DEST, connection) + source = self._makeOne(SOURCE) + dest = self._makeOne(DEST) blob = _Blob() - new_blob = source.copy_blob(blob, dest, NEW_NAME) + new_blob = source.copy_blob(blob, dest, NEW_NAME, + connection=connection) self.assertTrue(new_blob.bucket is dest) self.assertEqual(new_blob.name, NEW_NAME) kw, = connection._requested @@ -526,13 +530,14 @@ def __init__(self, bucket, name): self._bucket = bucket self._name = name - def upload_from_filename(self, filename): - _uploaded.append((self._bucket, self._name, filename)) + def upload_from_filename(self, filename, connection=None): + _uploaded.append((self._bucket, self._name, filename, + connection)) bucket = self._makeOne() with _Monkey(MUT, Blob=_Blob): bucket.upload_file(FILENAME) - self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME)]) + self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME, None)]) def test_upload_file_explicit_blob(self): from gcloud._testing import _Monkey @@ -547,13 +552,14 @@ def __init__(self, bucket, name): self._bucket = bucket self._name = name - def upload_from_filename(self, filename): - _uploaded.append((self._bucket, self._name, filename)) + def upload_from_filename(self, filename, connection=None): + _uploaded.append((self._bucket, self._name, filename, + connection)) bucket = self._makeOne() with _Monkey(MUT, Blob=_Blob): bucket.upload_file(FILENAME, BLOB_NAME) - self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILENAME)]) + self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILENAME, None)]) def test_upload_file_object_no_blob(self): from gcloud._testing import _Monkey @@ -568,13 +574,13 @@ def __init__(self, bucket, name): self._bucket = bucket self._name = name - def upload_from_file(self, fh): - _uploaded.append((self._bucket, self._name, fh)) + def upload_from_file(self, fh, connection=None): + _uploaded.append((self._bucket, self._name, fh, connection)) bucket = self._makeOne() with _Monkey(MUT, Blob=_Blob): found = bucket.upload_file_object(FILEOBJECT) - self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT)]) + self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT, None)]) self.assertTrue(isinstance(found, _Blob)) self.assertEqual(found._name, FILENAME) self.assertTrue(found._bucket is bucket) @@ -593,13 +599,13 @@ def __init__(self, bucket, name): self._bucket = bucket self._name = name - def upload_from_file(self, fh): - _uploaded.append((self._bucket, self._name, fh)) + def upload_from_file(self, fh, connection=None): + _uploaded.append((self._bucket, self._name, fh, connection)) bucket = self._makeOne() with _Monkey(MUT, Blob=_Blob): found = bucket.upload_file_object(FILEOBJECT, BLOB_NAME) - self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT)]) + self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT, None)]) self.assertTrue(isinstance(found, _Blob)) self.assertEqual(found._name, BLOB_NAME) self.assertTrue(found._bucket is bucket)