Skip to content

Commit

Permalink
Fix #137: default key based on filename.
Browse files Browse the repository at this point in the history
When no key is passed, guess it based on the basename of filename.
  • Loading branch information
tseaver committed Sep 26, 2014
1 parent 4c7fa75 commit e6ad297
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions gcloud/storage/bucket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from gcloud.storage import exceptions
from gcloud.storage.acl import BucketACL
from gcloud.storage.acl import DefaultObjectACL
Expand Down Expand Up @@ -230,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 @@ -246,8 +246,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 e6ad297

Please sign in to comment.