Skip to content

Commit d3bc612

Browse files
committed
Ensure blob/bucket upload methods return the blob.
Fixes #596.
1 parent 9518db7 commit d3bc612

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

gcloud/storage/blob.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
289289
:param size: The number of bytes to read from the file handle.
290290
If not provided, we'll try to guess the size using
291291
:func:`os.fstat`
292+
293+
:rtype: :class:`Blob`
294+
:returns: The updated Blob object.
292295
"""
293296
# Rewind the file if desired.
294297
if rewind:
@@ -335,6 +338,8 @@ def upload_from_file(self, file_obj, rewind=False, size=None,
335338
else:
336339
http_wrapper.MakeRequest(conn.http, request, retries=num_retries)
337340

341+
return self
342+
338343
def upload_from_filename(self, filename):
339344
"""Upload this blob's contents from the content of a named file.
340345
@@ -351,12 +356,17 @@ def upload_from_filename(self, filename):
351356
352357
:type filename: string
353358
:param filename: The path to the file.
359+
360+
:rtype: :class:`Blob`
361+
:returns: The updated Blob object.
354362
"""
355363
content_type, _ = mimetypes.guess_type(filename)
356364

357365
with open(filename, 'rb') as file_obj:
358366
self.upload_from_file(file_obj, content_type=content_type)
359367

368+
return self
369+
360370
def upload_from_string(self, data, content_type='text/plain'):
361371
"""Upload contents of this blob from the provided string.
362372

gcloud/storage/bucket.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ def upload_file(self, filename, blob=None):
405405
to put the file. If this is blank, we will try to
406406
upload the file to the root of the bucket with the
407407
same name as on your local file system.
408+
409+
:rtype: :class:`Blob`
410+
:returns: The updated Blob object.
408411
"""
409412
if blob is None:
410413
blob = os.path.basename(filename)
@@ -445,12 +448,16 @@ def upload_file_object(self, file_obj, blob=None):
445448
to put the file. If this is blank, we will try to
446449
upload the file to the root of the bucket with the
447450
same name as on your local file system.
451+
452+
:rtype: :class:`Blob`
453+
:returns: The updated Blob object.
448454
"""
449455
if blob:
450456
blob = self.new_blob(blob)
451457
else:
452458
blob = self.new_blob(os.path.basename(file_obj.name))
453-
return blob.upload_from_file(file_obj)
459+
blob.upload_from_file(file_obj)
460+
return blob
454461

455462
def get_cors(self):
456463
"""Retrieve CORS policies configured for this bucket.

gcloud/storage/test_blob.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ def test_upload_from_file_simple(self):
323323
with NamedTemporaryFile() as fh:
324324
fh.write(DATA)
325325
fh.flush()
326-
blob.upload_from_file(fh, rewind=True)
326+
found = blob.upload_from_file(fh, rewind=True)
327+
self.assertTrue(found is blob)
327328
rq = connection.http._requested
328329
self.assertEqual(len(rq), 1)
329330
self.assertEqual(rq[0]['method'], 'POST')
@@ -367,7 +368,8 @@ def test_upload_from_file_resumable(self):
367368
with NamedTemporaryFile() as fh:
368369
fh.write(DATA)
369370
fh.flush()
370-
blob.upload_from_file(fh, rewind=True)
371+
found = blob.upload_from_file(fh, rewind=True)
372+
self.assertTrue(found is blob)
371373
rq = connection.http._requested
372374
self.assertEqual(len(rq), 3)
373375
self.assertEqual(rq[0]['method'], 'POST')
@@ -422,7 +424,8 @@ def test_upload_from_file_w_slash_in_name(self):
422424
with NamedTemporaryFile() as fh:
423425
fh.write(DATA)
424426
fh.flush()
425-
blob.upload_from_file(fh, rewind=True)
427+
found = blob.upload_from_file(fh, rewind=True)
428+
self.assertTrue(found is blob)
426429
rq = connection.http._requested
427430
self.assertEqual(len(rq), 1)
428431
self.assertEqual(rq[0]['method'], 'POST')
@@ -462,7 +465,8 @@ def test_upload_from_filename(self):
462465
with NamedTemporaryFile(suffix='.jpeg') as fh:
463466
fh.write(DATA)
464467
fh.flush()
465-
blob.upload_from_filename(fh.name)
468+
found = blob.upload_from_filename(fh.name)
469+
self.assertTrue(found is blob)
466470
rq = connection.http._requested
467471
self.assertEqual(len(rq), 1)
468472
self.assertEqual(rq[0]['method'], 'POST')
@@ -498,7 +502,8 @@ def test_upload_from_string_w_bytes(self):
498502
bucket = _Bucket(connection)
499503
blob = self._makeOne(bucket, BLOB_NAME)
500504
blob.CHUNK_SIZE = 5
501-
blob.upload_from_string(DATA)
505+
found = blob.upload_from_string(DATA)
506+
self.assertTrue(found is blob)
502507
rq = connection.http._requested
503508
self.assertEqual(len(rq), 1)
504509
self.assertEqual(rq[0]['method'], 'POST')
@@ -536,7 +541,8 @@ def test_upload_from_string_w_text(self):
536541
bucket = _Bucket(connection)
537542
blob = self._makeOne(bucket, BLOB_NAME)
538543
blob.CHUNK_SIZE = 5
539-
blob.upload_from_string(DATA)
544+
found = blob.upload_from_string(DATA)
545+
self.assertTrue(found is blob)
540546
rq = connection.http._requested
541547
self.assertEqual(len(rq), 1)
542548
self.assertEqual(rq[0]['method'], 'POST')

gcloud/storage/test_bucket.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,11 @@ def upload_from_file(self, fh):
517517

518518
bucket = self._makeOne()
519519
with _Monkey(MUT, Blob=_Blob):
520-
bucket.upload_file_object(FILEOBJECT)
520+
found = bucket.upload_file_object(FILEOBJECT)
521521
self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT)])
522+
self.assertTrue(isinstance(found, _Blob))
523+
self.assertEqual(found._name, FILENAME)
524+
self.assertTrue(found._bucket is bucket)
522525

523526
def test_upload_file_object_explicit_blob(self):
524527
from gcloud._testing import _Monkey
@@ -539,8 +542,11 @@ def upload_from_file(self, fh):
539542

540543
bucket = self._makeOne()
541544
with _Monkey(MUT, Blob=_Blob):
542-
bucket.upload_file_object(FILEOBJECT, BLOB_NAME)
545+
found = bucket.upload_file_object(FILEOBJECT, BLOB_NAME)
543546
self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT)])
547+
self.assertTrue(isinstance(found, _Blob))
548+
self.assertEqual(found._name, BLOB_NAME)
549+
self.assertTrue(found._bucket is bucket)
544550

545551
def test_get_cors_eager(self):
546552
NAME = 'name'

0 commit comments

Comments
 (0)