Skip to content

Commit

Permalink
Putting optional connection on Bucket upload methods and copy_blob.
Browse files Browse the repository at this point in the history
Also adding stubs on some tests which were accidentally relying
on the implicit environment.
  • Loading branch information
dhermes committed Apr 28, 2015
1 parent dd1ae8d commit b282abb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
30 changes: 24 additions & 6 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
50 changes: 28 additions & 22 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit b282abb

Please sign in to comment.