Skip to content

Commit d8239f3

Browse files
committed
Rename upload/download methods to clarify directionality.
- 'set_contents*' -> 'upload'. - 'get_contents* -> 'download'. Original boto-like names retained as aliases per @jgeewax. Fixes #148.
1 parent d4c46af commit d8239f3

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

gcloud/storage/key.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ def delete(self):
162162
"""
163163
return self.bucket.delete_key(self)
164164

165-
def get_contents_to_file(self, file_obj):
166-
"""Gets the contents of this key to a file-like object.
165+
def download_to_file(self, file_obj):
166+
"""Download the contents of this key into a file-like object.
167167
168168
:type file_obj: file
169169
:param file_obj: A file handle to which to write the key's data.
@@ -173,31 +173,40 @@ def get_contents_to_file(self, file_obj):
173173
for chunk in KeyDataIterator(self):
174174
file_obj.write(chunk)
175175

176-
def get_contents_to_filename(self, filename):
177-
"""Get the contents of this key to a file by name.
176+
# NOTE: Alias for boto-like API.
177+
get_contents_to_file = download_to_file
178+
179+
def download_to_filename(self, filename):
180+
"""Download the contents of this key into a named file.
178181
179182
:type filename: string
180183
:param filename: A filename to be passed to ``open``.
181184
182185
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
183186
"""
184187
with open(filename, 'wb') as file_obj:
185-
self.get_contents_to_file(file_obj)
188+
self.download_to_file(file_obj)
189+
190+
# NOTE: Alias for boto-like API.
191+
get_contents_to_filename = download_to_filename
186192

187-
def get_contents_as_string(self):
188-
"""Gets the data stored on this Key as a string.
193+
def download_as_string(self):
194+
"""Download the contents of this key as a string.
189195
190196
:rtype: string
191197
:returns: The data stored in this key.
192198
:raises: :class:`gcloud.storage.exceptions.NotFoundError`
193199
"""
194200
string_buffer = StringIO()
195-
self.get_contents_to_file(string_buffer)
201+
self.download_to_file(string_buffer)
196202
return string_buffer.getvalue()
197203

198-
def set_contents_from_file(self, file_obj, rewind=False, size=None,
199-
content_type=None):
200-
"""Set the contents of this key to the contents of a file handle.
204+
# NOTE: Alias for boto-like API.
205+
get_contents_as_string = download_as_string
206+
207+
def upload_from_file(self, file_obj, rewind=False, size=None,
208+
content_type=None):
209+
"""Upload the contents of this key from a file-like object.
201210
202211
:type file_obj: file
203212
:param file_obj: A file handle open for reading.
@@ -255,30 +264,25 @@ def set_contents_from_file(self, file_obj, rewind=False, size=None,
255264

256265
bytes_uploaded += chunk_size
257266

258-
def set_contents_from_filename(self, filename):
259-
"""Open a path and set this key's contents to the content of that file.
267+
# NOTE: Alias for boto-like API.
268+
set_contents_from_file = upload_from_file
269+
270+
def upload_from_filename(self, filename):
271+
"""Upload this key's contents from the content of f named file.
260272
261273
:type filename: string
262274
:param filename: The path to the file.
263275
"""
264276
content_type, _ = mimetypes.guess_type(filename)
265277

266278
with open(filename, 'rb') as file_obj:
267-
self.set_contents_from_file(file_obj, content_type=content_type)
279+
self.upload_from_file(file_obj, content_type=content_type)
268280

269-
def set_contents_from_string(self, data, content_type='text/plain'):
270-
"""Sets the contents of this key to the provided string.
281+
# NOTE: Alias for boto-like API.
282+
set_contents_from_filename = upload_from_filename
271283

272-
You can use this method to quickly set the value of a key::
273-
274-
>>> from gcloud import storage
275-
>>> connection = storage.get_connection(project, email, key_path)
276-
>>> bucket = connection.get_bucket(bucket_name)
277-
>>> key = bucket.new_key('my_text_file.txt')
278-
>>> key.set_contents_from_string('This is the contents of my file!')
279-
280-
Under the hood this is using a string buffer and calling
281-
:func:`gcloud.storage.key.Key.set_contents_from_file`.
284+
def upload_from_string(self, data, content_type='text/plain'):
285+
"""Upload contents of this key from the provided string.
282286
283287
:type data: string
284288
:param data: The data to store in this key.
@@ -293,6 +297,9 @@ def set_contents_from_string(self, data, content_type='text/plain'):
293297
content_type=content_type)
294298
return self
295299

300+
# NOTE: Alias for boto-like API.
301+
set_contents_from_string = upload_from_string
302+
296303
def has_metadata(self, field=None):
297304
"""Check if metadata is available locally.
298305

gcloud/storage/test_key.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_delete(self):
142142
key.delete()
143143
self.assertFalse(key.exists())
144144

145-
def test_get_contents_to_file(self):
145+
def test_download_to_file(self):
146146
from StringIO import StringIO
147147
from gcloud._testing import _Monkey
148148
from gcloud.storage import key as MUT
@@ -153,10 +153,10 @@ def test_get_contents_to_file(self):
153153
key = self._makeOne(bucket, KEY)
154154
fh = StringIO()
155155
with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)):
156-
key.get_contents_to_file(fh)
156+
key.download_to_file(fh)
157157
self.assertEqual(fh.getvalue(), ''.join(_CHUNKS))
158158

159-
def test_get_contents_to_filename(self):
159+
def test_download_to_filename(self):
160160
from tempfile import NamedTemporaryFile
161161
from gcloud._testing import _Monkey
162162
from gcloud.storage import key as MUT
@@ -167,13 +167,13 @@ def test_get_contents_to_filename(self):
167167
key = self._makeOne(bucket, KEY)
168168
with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)):
169169
with NamedTemporaryFile() as f:
170-
key.get_contents_to_filename(f.name)
170+
key.download_to_filename(f.name)
171171
f.flush()
172172
with open(f.name) as g:
173173
wrote = g.read()
174174
self.assertEqual(wrote, ''.join(_CHUNKS))
175175

176-
def test_get_contents_as_string(self):
176+
def test_download_as_string(self):
177177
from gcloud._testing import _Monkey
178178
from gcloud.storage import key as MUT
179179
_CHUNKS = ['abc', 'def']
@@ -182,10 +182,10 @@ def test_get_contents_as_string(self):
182182
bucket = _Bucket(connection)
183183
key = self._makeOne(bucket, KEY)
184184
with _Monkey(MUT, KeyDataIterator=lambda self: iter(_CHUNKS)):
185-
fetched = key.get_contents_as_string()
185+
fetched = key.download_as_string()
186186
self.assertEqual(fetched, ''.join(_CHUNKS))
187187

188-
def test_set_contents_from_file(self):
188+
def test_upload_from_file(self):
189189
from tempfile import NamedTemporaryFile
190190
from urlparse import parse_qsl
191191
from urlparse import urlsplit
@@ -206,7 +206,7 @@ def test_set_contents_from_file(self):
206206
with NamedTemporaryFile() as fh:
207207
fh.write(DATA)
208208
fh.flush()
209-
key.set_contents_from_file(fh, rewind=True)
209+
key.upload_from_file(fh, rewind=True)
210210
rq = connection._requested
211211
self.assertEqual(len(rq), 3)
212212
self.assertEqual(rq[0]['method'], 'POST')
@@ -231,7 +231,7 @@ def test_set_contents_from_file(self):
231231
self.assertEqual(rq[2]['data'], DATA[5:])
232232
self.assertEqual(rq[2]['headers'], {'Content-Range': 'bytes 5-5/6'})
233233

234-
def test_set_contents_from_filename(self):
234+
def test_upload_from_filename(self):
235235
from tempfile import NamedTemporaryFile
236236
from urlparse import parse_qsl
237237
from urlparse import urlsplit
@@ -252,7 +252,7 @@ def test_set_contents_from_filename(self):
252252
with NamedTemporaryFile(suffix='.jpeg') as fh:
253253
fh.write(DATA)
254254
fh.flush()
255-
key.set_contents_from_filename(fh.name)
255+
key.upload_from_filename(fh.name)
256256
rq = connection._requested
257257
self.assertEqual(len(rq), 3)
258258
self.assertEqual(rq[0]['method'], 'POST')
@@ -277,7 +277,7 @@ def test_set_contents_from_filename(self):
277277
self.assertEqual(rq[2]['data'], DATA[5:])
278278
self.assertEqual(rq[2]['headers'], {'Content-Range': 'bytes 5-5/6'})
279279

280-
def test_set_contents_from_string(self):
280+
def test_upload_from_string(self):
281281
from urlparse import parse_qsl
282282
from urlparse import urlsplit
283283
KEY = 'key'
@@ -294,7 +294,7 @@ def test_set_contents_from_string(self):
294294
bucket = _Bucket(connection)
295295
key = self._makeOne(bucket, KEY)
296296
key.CHUNK_SIZE = 5
297-
key.set_contents_from_string(DATA)
297+
key.upload_from_string(DATA)
298298
rq = connection._requested
299299
self.assertEqual(len(rq), 3)
300300
self.assertEqual(rq[0]['method'], 'POST')

0 commit comments

Comments
 (0)