Skip to content

Commit d981af6

Browse files
committed
Removing of Bucket.new_blob.
Opting to just use Blob() constructor where relevant.
1 parent fb09637 commit d981af6

File tree

6 files changed

+32
-71
lines changed

6 files changed

+32
-71
lines changed

docs/_components/storage-getting-started.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ Python built-in ``object``.
9191
If you want to set some data, you just create a ``Blob`` inside your bucket
9292
and store your data inside the blob::
9393

94-
>>> blob = bucket.new_blob('greeting.txt')
94+
>>> blob = storage.Blob('greeting.txt', bucket=bucket)
9595
>>> blob.upload_from_string('Hello world!')
9696

97-
:func:`new_blob <gcloud.storage.bucket.Bucket.new_blob>` creates a
98-
:class:`Blob <gcloud.storage.blob.Blob>` object locally and
97+
This creates a :class:`Blob <gcloud.storage.blob.Blob>` object locally and
9998
:func:`upload_from_string <gcloud.storage.blob.Blob.upload_from_string>`
10099
allows you to put a string into the blob.
101100

@@ -117,8 +116,7 @@ Then you can look at the file in a terminal::
117116
And what about when you're not dealing with text?
118117
That's pretty simple too::
119118

120-
>>> blob = bucket.new_blob('kitten.jpg')
121-
>>> blob.upload_from_filename('kitten.jpg')
119+
>>> blob = bucket.upload_file('kitten.jpg')
122120

123121
And to test whether it worked?
124122

@@ -136,9 +134,9 @@ remotely. If the blob doesn't exist, it will return ``None``.
136134
.. note:: ``get_blob`` is **not** retrieving the entire object's data.
137135

138136
If you want to "get-or-create" the blob (that is, overwrite it if it
139-
already exists), you can use :func:`new_blob
140-
<gcloud.storage.bucket.Bucket.new_blob>`. However, keep in mind, the blob
141-
is not created until you store some data inside of it.
137+
already exists), you can just construct a :class:`gcloud.storage.blob.Blob`
138+
locally and update it. However, keep in mind, the blob is not created
139+
remotely until you store some data inside of it.
142140

143141
If you want to check whether a blob exists, you can use the ``in`` operator
144142
in Python::

docs/_components/storage-quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ you can create buckets and blobs::
6161
>>> bucket = storage.create_bucket('my-new-bucket', connection=connection)
6262
>>> print bucket
6363
<Bucket: my-new-bucket>
64-
>>> blob = bucket.new_blob('my-test-file.txt')
64+
>>> blob = storage.Blob('my-test-file.txt', bucket=bucket)
6565
>>> print blob
6666
<Blob: my-new-bucket, my-test-file.txt>
6767
>>> blob = blob.upload_from_string('this is test content!')

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ Cloud Storage
4949
from gcloud import storage
5050
storage.set_defaults()
5151
bucket = storage.get_bucket('<your-bucket-name>')
52-
blob = bucket.new_blob('my-test-file.txt')
52+
blob = storage.Blob('my-test-file.txt', bucket=bucket)
5353
blob = blob.upload_contents_from_string('this is test content!')

gcloud/storage/bucket.py

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -241,27 +241,6 @@ def iterator(self, prefix=None, delimiter=None, max_results=None,
241241

242242
return self._iterator_class(self, extra_params=extra_params)
243243

244-
def new_blob(self, blob):
245-
"""Given path name (or Blob), return a :class:`Blob` object.
246-
247-
This is really useful when you're not sure if you have a ``Blob``
248-
instance or a string path name. Given either of those types, this
249-
returns the corresponding ``Blob``.
250-
251-
:type blob: string or :class:`gcloud.storage.blob.Blob`
252-
:param blob: A path name or actual blob object.
253-
254-
:rtype: :class:`gcloud.storage.blob.Blob`
255-
:returns: A Blob object with the path provided.
256-
"""
257-
if isinstance(blob, Blob):
258-
return blob
259-
260-
if isinstance(blob, six.string_types):
261-
return Blob(bucket=self, name=blob)
262-
263-
raise TypeError('Invalid blob: %s' % blob)
264-
265244
def delete(self, force=False):
266245
"""Delete this bucket.
267246
@@ -379,12 +358,12 @@ def copy_blob(self, blob, destination_bucket, new_name=None):
379358
"""
380359
if new_name is None:
381360
new_name = blob.name
382-
new_blob = destination_bucket.new_blob(new_name)
361+
new_blob = Blob(bucket=destination_bucket, name=new_name)
383362
api_path = blob.path + '/copyTo' + new_blob.path
384363
self.connection.api_request(method='POST', path=api_path)
385364
return new_blob
386365

387-
def upload_file(self, filename, blob=None):
366+
def upload_file(self, filename, blob_name=None):
388367
"""Shortcut method to upload a file into this bucket.
389368
390369
Use this method to quickly put a local file in Cloud Storage.
@@ -398,9 +377,8 @@ def upload_file(self, filename, blob=None):
398377
>>> print bucket.get_all_blobs()
399378
[<Blob: my-bucket, remote-text-file.txt>]
400379
401-
If you don't provide a blob value, we will try to upload the file
402-
using the local filename as the blob (**not** the complete
403-
path)::
380+
If you don't provide a blob name, we will try to upload the file
381+
using the local filename (**not** the complete path)::
404382
405383
>>> from gcloud import storage
406384
>>> connection = storage.get_connection(project)
@@ -412,22 +390,22 @@ def upload_file(self, filename, blob=None):
412390
:type filename: string
413391
:param filename: Local path to the file you want to upload.
414392
415-
:type blob: string or :class:`gcloud.storage.blob.Blob`
416-
:param blob: The blob (either an object or a remote path) of where
417-
to put the file. If this is blank, we will try to
418-
upload the file to the root of the bucket with the
419-
same name as on your local file system.
393+
:type blob_name: string
394+
:param blob_name: The name of the blob to upload the file to. If this
395+
is blank, we will try to upload the file to the root
396+
of the bucket with the same name as on your local
397+
file system.
420398
421399
:rtype: :class:`Blob`
422400
:returns: The updated Blob object.
423401
"""
424-
if blob is None:
425-
blob = os.path.basename(filename)
426-
blob = self.new_blob(blob)
402+
if blob_name is None:
403+
blob_name = os.path.basename(filename)
404+
blob = Blob(bucket=self, name=blob_name)
427405
blob.upload_from_filename(filename)
428406
return blob
429407

430-
def upload_file_object(self, file_obj, blob=None):
408+
def upload_file_object(self, file_obj, blob_name=None):
431409
"""Shortcut method to upload a file object into this bucket.
432410
433411
Use this method to quickly put a local file in Cloud Storage.
@@ -441,9 +419,8 @@ def upload_file_object(self, file_obj, blob=None):
441419
>>> print bucket.get_all_blobs()
442420
[<Blob: my-bucket, remote-text-file.txt>]
443421
444-
If you don't provide a blob value, we will try to upload the file
445-
using the local filename as the blob (**not** the complete
446-
path)::
422+
If you don't provide a blob name, we will try to upload the file
423+
using the local filename (**not** the complete path)::
447424
448425
>>> from gcloud import storage
449426
>>> connection = storage.get_connection(project)
@@ -455,19 +432,18 @@ def upload_file_object(self, file_obj, blob=None):
455432
:type file_obj: file
456433
:param file_obj: A file handle open for reading.
457434
458-
:type blob: string or :class:`gcloud.storage.blob.Blob`
459-
:param blob: The blob (either an object or a remote path) of where
460-
to put the file. If this is blank, we will try to
461-
upload the file to the root of the bucket with the
462-
same name as on your local file system.
435+
:type blob_name: string
436+
:param blob_name: The name of the blob to upload the file to. If this
437+
is blank, we will try to upload the file to the root
438+
of the bucket with the same name as on your local
439+
file system.
463440
464441
:rtype: :class:`Blob`
465442
:returns: The updated Blob object.
466443
"""
467-
if blob:
468-
blob = self.new_blob(blob)
469-
else:
470-
blob = self.new_blob(os.path.basename(file_obj.name))
444+
if blob_name is None:
445+
blob_name = os.path.basename(file_obj.name)
446+
blob = Blob(bucket=self, name=blob_name)
471447
blob.upload_from_file(file_obj)
472448
return blob
473449

gcloud/storage/demo/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
print(list(storage.get_all_buckets(connection)))
2525

2626
# How about we create a new blob inside this bucket.
27-
blob = bucket.new_blob("my-new-file.txt")
27+
blob = storage.Blob("my-new-file.txt", bucket=bucket)
2828

2929
# Now let's put some data in there.
3030
blob.upload_from_string("this is some data!")

gcloud/storage/test_bucket.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,6 @@ def test_iterator_explicit(self):
306306
self.assertEqual(kw['path'], '/b/%s/o' % NAME)
307307
self.assertEqual(kw['query_params'], EXPECTED)
308308

309-
def test_new_blob_str(self):
310-
from gcloud.storage.blob import Blob
311-
BLOB_NAME = 'blob-name'
312-
bucket = self._makeOne()
313-
blob = bucket.new_blob(BLOB_NAME)
314-
self.assertTrue(isinstance(blob, Blob))
315-
self.assertTrue(blob.bucket is bucket)
316-
self.assertEqual(blob.name, BLOB_NAME)
317-
318-
def test_new_blob_invalid(self):
319-
bucket = self._makeOne()
320-
self.assertRaises(TypeError, bucket.new_blob, object())
321-
322309
def test_delete_default_miss(self):
323310
from gcloud.exceptions import NotFound
324311
NAME = 'name'

0 commit comments

Comments
 (0)