Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #137: default key based on filename. #191

Merged
merged 1 commit into from
Sep 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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