Skip to content

Commit f634292

Browse files
committed
Merge pull request #1002 from dhermes/remove-redundant-storage-method
Removing redundant Bucket.upload* methods.
2 parents 712744f + c54a8d0 commit f634292

File tree

7 files changed

+20
-205
lines changed

7 files changed

+20
-205
lines changed

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ how to create a bucket.
107107
blob = bucket.get_blob('/remote/path/to/file.txt')
108108
print blob.download_as_string()
109109
blob.upload_from_string('New contents!')
110-
bucket.upload_file('/remote/path/storage.txt', '/local/path.txt')
110+
blob2 = storage.Blob('/remote/path/storage.txt', bucket)
111+
blob2.upload_from_filename(filename='/local/path.txt')
111112
112113
Contributing
113114
------------

docs/_components/storage-getting-started.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ Then you can look at the file in a terminal::
116116
And what about when you're not dealing with text?
117117
That's pretty simple too::
118118

119-
>>> blob = bucket.upload_file('kitten.jpg')
119+
>>> blob = storage.Blob('kitten.jpg', bucket)
120+
>>> blob.upload_from_filename('kitten.jpg')
120121

121122
And to test whether it worked?
122123

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ Cloud Storage
5555
client = storage.Client()
5656
bucket = client.get_bucket('<your-bucket-name>')
5757
blob = storage.Blob('my-test-file.txt', bucket=bucket)
58-
blob = blob.upload_contents_from_string('this is test content!')
58+
blob.upload_from_string('this is test content!')

gcloud/storage/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
>>> blob = bucket.get_blob('/remote/path/to/file.txt')
2424
>>> print blob.download_as_string()
2525
>>> blob.upload_from_string('New contents!')
26-
>>> bucket.upload_file('/remote/path/storage.txt', '/local/path.txt')
26+
>>> blob2 = storage.Blob('/remote/path/storage.txt', bucket)
27+
>>> blob2.upload_from_filename(filename='/local/path.txt')
2728
2829
The main concepts with this API are:
2930

gcloud/storage/bucket.py

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import datetime
1818
import copy
19-
import os
2019

2120
import pytz
2221
import six
@@ -442,98 +441,6 @@ def copy_blob(self, blob, destination_bucket, new_name=None,
442441
new_blob._set_properties(copy_result)
443442
return new_blob
444443

445-
def upload_file(self, filename, blob_name=None, client=None):
446-
"""Shortcut method to upload a file into this bucket.
447-
448-
Use this method to quickly put a local file in Cloud Storage.
449-
450-
For example::
451-
452-
>>> from gcloud import storage
453-
>>> client = storage.Client()
454-
>>> bucket = client.get_bucket('my-bucket')
455-
>>> bucket.upload_file('~/my-file.txt', 'remote-text-file.txt')
456-
>>> print bucket.list_blobs()
457-
[<Blob: my-bucket, remote-text-file.txt>]
458-
459-
If you don't provide a blob name, we will try to upload the file
460-
using the local filename (**not** the complete path)::
461-
462-
>>> from gcloud import storage
463-
>>> client = storage.Client()
464-
>>> bucket = client.get_bucket('my-bucket')
465-
>>> bucket.upload_file('~/my-file.txt')
466-
>>> print bucket.list_blobs()
467-
[<Blob: my-bucket, my-file.txt>]
468-
469-
:type filename: string
470-
:param filename: Local path to the file you want to upload.
471-
472-
:type blob_name: string
473-
:param blob_name: The name of the blob to upload the file to. If this
474-
is blank, we will try to upload the file to the root
475-
of the bucket with the same name as on your local
476-
file system.
477-
478-
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
479-
:param client: Optional. The client to use. If not passed, falls back
480-
to the ``client`` stored on the current bucket.
481-
482-
:rtype: :class:`Blob`
483-
:returns: The updated Blob object.
484-
"""
485-
if blob_name is None:
486-
blob_name = os.path.basename(filename)
487-
blob = Blob(bucket=self, name=blob_name)
488-
blob.upload_from_filename(filename, client=client)
489-
return blob
490-
491-
def upload_file_object(self, file_obj, blob_name=None, client=None):
492-
"""Shortcut method to upload a file object into this bucket.
493-
494-
Use this method to quickly put a local file in Cloud Storage.
495-
496-
For example::
497-
498-
>>> from gcloud import storage
499-
>>> client = storage.Client()
500-
>>> bucket = client.get_bucket('my-bucket')
501-
>>> bucket.upload_file(open('~/my-file.txt'), 'remote-text-file.txt')
502-
>>> print bucket.list_blobs()
503-
[<Blob: my-bucket, remote-text-file.txt>]
504-
505-
If you don't provide a blob name, we will try to upload the file
506-
using the local filename (**not** the complete path)::
507-
508-
>>> from gcloud import storage
509-
>>> client = storage.Client()
510-
>>> bucket = client.get_bucket('my-bucket')
511-
>>> bucket.upload_file(open('~/my-file.txt'))
512-
>>> print bucket.list_blobs()
513-
[<Blob: my-bucket, my-file.txt>]
514-
515-
:type file_obj: file
516-
:param file_obj: A file handle open for reading.
517-
518-
:type blob_name: string
519-
:param blob_name: The name of the blob to upload the file to. If this
520-
is blank, we will try to upload the file to the root
521-
of the bucket with the same name as on your local
522-
file system.
523-
524-
:type client: :class:`gcloud.storage.client.Client` or ``NoneType``
525-
:param client: Optional. The client to use. If not passed, falls back
526-
to the ``client`` stored on the current bucket.
527-
528-
:rtype: :class:`Blob`
529-
:returns: The updated Blob object.
530-
"""
531-
if blob_name is None:
532-
blob_name = os.path.basename(file_obj.name)
533-
blob = Blob(bucket=self, name=blob_name)
534-
blob.upload_from_file(file_obj, client=client)
535-
return blob
536-
537444
@property
538445
def cors(self):
539446
"""Retrieve CORS policies configured for this bucket.

gcloud/storage/test_bucket.py

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import io
16-
1715
import unittest2
1816

1917

@@ -506,99 +504,6 @@ class _Blob(object):
506504
self.assertEqual(kw['method'], 'POST')
507505
self.assertEqual(kw['path'], COPY_PATH)
508506

509-
def test_upload_file_default_blob_name(self):
510-
from gcloud._testing import _Monkey
511-
from gcloud.storage import bucket as MUT
512-
BASENAME = 'file.ext'
513-
FILENAME = '/path/to/%s' % BASENAME
514-
_uploaded = []
515-
516-
class _Blob(object):
517-
518-
def __init__(self, bucket, name):
519-
self._bucket = bucket
520-
self._name = name
521-
522-
def upload_from_filename(self, filename, client=None):
523-
_uploaded.append((self._bucket, self._name, filename,
524-
client))
525-
526-
bucket = self._makeOne()
527-
with _Monkey(MUT, Blob=_Blob):
528-
bucket.upload_file(FILENAME)
529-
self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME, None)])
530-
531-
def test_upload_file_blob_w_blob_name(self):
532-
from gcloud._testing import _Monkey
533-
from gcloud.storage import bucket as MUT
534-
FILENAME = '/path/to/file'
535-
BLOB_NAME = 'blob-name'
536-
_uploaded = []
537-
538-
class _Blob(object):
539-
540-
def __init__(self, bucket, name):
541-
self._bucket = bucket
542-
self._name = name
543-
544-
def upload_from_filename(self, filename, client=None):
545-
_uploaded.append((self._bucket, self._name, filename,
546-
client))
547-
548-
bucket = self._makeOne()
549-
with _Monkey(MUT, Blob=_Blob):
550-
bucket.upload_file(FILENAME, BLOB_NAME)
551-
self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILENAME, None)])
552-
553-
def test_upload_file_object_no_blob(self):
554-
from gcloud._testing import _Monkey
555-
from gcloud.storage import bucket as MUT
556-
FILENAME = 'file.txt'
557-
FILEOBJECT = MockFile(FILENAME)
558-
_uploaded = []
559-
560-
class _Blob(object):
561-
562-
def __init__(self, bucket, name):
563-
self._bucket = bucket
564-
self._name = name
565-
566-
def upload_from_file(self, fh, client=None):
567-
_uploaded.append((self._bucket, self._name, fh, client))
568-
569-
bucket = self._makeOne()
570-
with _Monkey(MUT, Blob=_Blob):
571-
found = bucket.upload_file_object(FILEOBJECT)
572-
self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT, None)])
573-
self.assertTrue(isinstance(found, _Blob))
574-
self.assertEqual(found._name, FILENAME)
575-
self.assertTrue(found._bucket is bucket)
576-
577-
def test_upload_file_object_blob(self):
578-
from gcloud._testing import _Monkey
579-
from gcloud.storage import bucket as MUT
580-
FILENAME = 'file.txt'
581-
FILEOBJECT = MockFile(FILENAME)
582-
BLOB_NAME = 'blob-name'
583-
_uploaded = []
584-
585-
class _Blob(object):
586-
587-
def __init__(self, bucket, name):
588-
self._bucket = bucket
589-
self._name = name
590-
591-
def upload_from_file(self, fh, client=None):
592-
_uploaded.append((self._bucket, self._name, fh, client))
593-
594-
bucket = self._makeOne()
595-
with _Monkey(MUT, Blob=_Blob):
596-
found = bucket.upload_file_object(FILEOBJECT, BLOB_NAME)
597-
self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT, None)])
598-
self.assertTrue(isinstance(found, _Blob))
599-
self.assertEqual(found._name, BLOB_NAME)
600-
self.assertTrue(found._bucket is bucket)
601-
602507
def test_etag(self):
603508
ETAG = 'ETAG'
604509
properties = {'etag': ETAG}
@@ -1018,14 +923,6 @@ def __init__(self, client=None):
1018923
self.client = client
1019924

1020925

1021-
class MockFile(io.StringIO):
1022-
name = None
1023-
1024-
def __init__(self, name, buffer_=None):
1025-
super(MockFile, self).__init__(buffer_)
1026-
self.name = name
1027-
1028-
1029926
class _Client(object):
1030927

1031928
def __init__(self, connection, project=None):

system_tests/storage.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import httplib2
16+
import os
1617
import six
1718
import tempfile
1819
import time
@@ -141,7 +142,11 @@ def test_small_file_write_from_filename(self):
141142
self.assertEqual(md5_hash, file_data['hash'])
142143

143144
def test_write_metadata(self):
144-
blob = self.bucket.upload_file(self.FILES['logo']['path'])
145+
filename = self.FILES['logo']['path']
146+
blob_name = os.path.basename(filename)
147+
148+
blob = storage.Blob(blob_name, bucket=self.bucket)
149+
blob.upload_from_filename(filename)
145150
self.case_blobs_to_delete.append(blob)
146151

147152
# NOTE: This should not be necessary. We should be able to pass
@@ -167,8 +172,9 @@ def test_direct_write_and_read_into_file(self):
167172
self.assertEqual(file_contents, stored_contents)
168173

169174
def test_copy_existing_file(self):
170-
blob = self.bucket.upload_file(self.FILES['logo']['path'],
171-
blob_name='CloudLogo')
175+
filename = self.FILES['logo']['path']
176+
blob = storage.Blob('CloudLogo', bucket=self.bucket)
177+
blob.upload_from_filename(filename)
172178
self.case_blobs_to_delete.append(blob)
173179

174180
new_blob = self.bucket.copy_blob(blob, self.bucket, 'CloudLogoCopy')
@@ -191,7 +197,8 @@ def setUpClass(cls):
191197
blob.delete()
192198

193199
logo_path = cls.FILES['logo']['path']
194-
blob = cls.bucket.upload_file(logo_path, blob_name=cls.FILENAMES[0])
200+
blob = storage.Blob(cls.FILENAMES[0], bucket=cls.bucket)
201+
blob.upload_from_filename(logo_path)
195202
cls.suite_blobs_to_delete = [blob]
196203

197204
# Copy main blob onto remaining in FILENAMES.
@@ -242,7 +249,8 @@ def setUpClass(cls):
242249
blob.delete()
243250

244251
simple_path = cls.FILES['simple']['path']
245-
blob = cls.bucket.upload_file(simple_path, blob_name=cls.FILENAMES[0])
252+
blob = storage.Blob(cls.FILENAMES[0], bucket=cls.bucket)
253+
blob.upload_from_filename(simple_path)
246254
cls.suite_blobs_to_delete = [blob]
247255
for filename in cls.FILENAMES[1:]:
248256
new_blob = cls.bucket.copy_blob(blob, cls.bucket, filename)

0 commit comments

Comments
 (0)