From 98d7a50790995473e53afd033ba9a2da19a5e460 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Sat, 13 Apr 2013 19:00:37 -0400 Subject: [PATCH] Unquote s3 key names. Closes #13. --- moto/s3/models.py | 7 +++++++ moto/s3/utils.py | 5 +++++ tests/test_s3/test_s3.py | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/moto/s3/models.py b/moto/s3/models.py index d8cd06103..644edfb95 100644 --- a/moto/s3/models.py +++ b/moto/s3/models.py @@ -2,6 +2,7 @@ import datetime import md5 from moto.core import BaseBackend +from .utils import clean_key_name class FakeKey(object): @@ -72,6 +73,8 @@ class S3Backend(BaseBackend): return None def set_key(self, bucket_name, key_name, value): + key_name = clean_key_name(key_name) + bucket = self.buckets[bucket_name] new_key = FakeKey(name=key_name, value=value) bucket.keys[key_name] = new_key @@ -79,6 +82,7 @@ class S3Backend(BaseBackend): return new_key def get_key(self, bucket_name, key_name): + key_name = clean_key_name(key_name) bucket = self.get_bucket(bucket_name) if bucket: return bucket.keys.get(key_name) @@ -107,10 +111,13 @@ class S3Backend(BaseBackend): return key_results, folder_results def delete_key(self, bucket_name, key_name): + key_name = clean_key_name(key_name) bucket = self.buckets[bucket_name] return bucket.keys.pop(key_name) def copy_key(self, src_bucket_name, src_key_name, dest_bucket_name, dest_key_name): + src_key_name = clean_key_name(src_key_name) + dest_key_name = clean_key_name(dest_key_name) src_bucket = self.buckets[src_bucket_name] dest_bucket = self.buckets[dest_bucket_name] dest_bucket.keys[dest_key_name] = src_bucket.keys[src_key_name] diff --git a/moto/s3/utils.py b/moto/s3/utils.py index 8786585f5..d9e5671e9 100644 --- a/moto/s3/utils.py +++ b/moto/s3/utils.py @@ -1,4 +1,5 @@ import re +import urllib2 import urlparse bucket_name_regex = re.compile("(.+).s3.amazonaws.com") @@ -21,3 +22,7 @@ def bucket_name_from_hostname(hostname): else: # No subdomain found. return None + + +def clean_key_name(key_name): + return urllib2.unquote(key_name) diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index d4ac1170b..a12ea9c30 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -205,3 +205,16 @@ def test_bucket_name_with_dot(): k = Key(bucket, 'somekey') k.set_contents_from_string('somedata') + + +@mock_s3 +def test_key_with_special_characters(): + conn = boto.connect_s3() + bucket = conn.create_bucket('test_bucket_name') + + key = Key(bucket, 'test_list_keys_2/x?y') + key.set_contents_from_string('value1') + + key_list = bucket.list('test_list_keys_2/', '/') + keys = [x for x in key_list] + keys[0].name.should.equal("test_list_keys_2/x?y")