Skip to content

Commit

Permalink
Merge pull request #191 from tseaver/137-upload_file-guess_key_based_…
Browse files Browse the repository at this point in the history
…on_filename

Fix #137:  default key based on filename.
  • Loading branch information
silvolu committed Sep 30, 2014
2 parents 62e887d + e6ad297 commit 60887ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ def upload_file(self, filename, key=None):
to the root of the bucket
with the same name as on your local file system.
"""
if key is None:
key = os.path.basename(filename)
key = self.new_key(key)
return key.set_contents_from_filename(filename)

Expand Down
18 changes: 16 additions & 2 deletions gcloud/storage/test_bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,22 @@ def test_delete_keys_miss(self):
self.assertEqual(kw[1]['method'], 'DELETE')
self.assertEqual(kw[1]['path'], '/b/%s/o/%s' % (NAME, NONESUCH))

# See: https://github.com/GoogleCloudPlatform/gcloud-python/issues/137
#def test_upload_file_default_key(self):
def test_upload_file_default_key(self):
from gcloud.test_credentials import _Monkey
from gcloud.storage import bucket as MUT
BASENAME = 'file.ext'
FILENAME = '/path/to/%s' % BASENAME
_uploaded = []
class _Key(object):
def __init__(self, bucket, name):
self._bucket = bucket
self._name = name
def set_contents_from_filename(self, filename):
_uploaded.append((self._bucket, self._name, filename))
bucket = self._makeOne()
with _Monkey(MUT, Key=_Key):
bucket.upload_file(FILENAME)
self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME)])

def test_upload_file_explicit_key(self):
from gcloud.test_credentials import _Monkey
Expand Down

0 comments on commit 60887ab

Please sign in to comment.