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

Handle S3 metadata input/output #274

Merged
merged 2 commits into from
Apr 28, 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
15 changes: 15 additions & 0 deletions botocore/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,21 @@ def build_parameter_json(self, value, built_params, label=''):
for key in value:
new_value[key] = value[key]

def build_parameter_rest(self, style, value, built_params, label=''):
# There's a special case for rest-xml with header locations
# that we we need to handle for maps. If this is not the
# case we can defer to the base class's implementation of
# map parameters serialization.
if style == 'rest-xml' and getattr(self, 'location', '') == 'header':
prefix = getattr(self, 'location_name', '') or self.name
user_params = value
for key, value in user_params.items():
full_key_name = prefix + key
built_params['headers'][full_key_name] = value
else:
return super(MapParameter, self).build_parameter_rest(
style, value, build_params, label)


class StructParameter(Parameter):

Expand Down
16 changes: 15 additions & 1 deletion botocore/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,23 @@ def merge_header_values(self, headers):
location = member.get('location')
if location == 'header':
location_name = member.get('location_name')
if location_name in headers:
if member['type'] == 'map':
self._merge_map_header_values(headers, location_name,
member_name, member)
elif location_name in headers:
self.value[member_name] = headers[location_name]

def _merge_map_header_values(self, headers, location_name,
member_name, member):
final_map_value = {}
for header_name in headers:
if header_name.startswith(location_name):
header_value = headers[header_name]
actual_name = header_name[len(location_name):]
final_map_value[actual_name] = header_value
if final_map_value:
self.value[member_name] = final_map_value


class JSONResponse(Response):

Expand Down
13 changes: 13 additions & 0 deletions tests/integration/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,19 @@ def test_copy_with_quoted_char(self):
data = response[1]
self.assertEqual(data['Body'].read().decode('utf-8'), 'foo')

def test_copy_with_s3_metadata(self):
key_name = 'foo.txt'
self.create_object(key_name=key_name)
copied_key = 'copied.txt'
operation = self.service.get_operation('CopyObject')
http, parsed = operation.call(
self.endpoint, bucket=self.bucket_name, key=copied_key,
copy_source='%s/%s' % (self.bucket_name, key_name),
metadata_directive='REPLACE',
metadata={"mykey": "myvalue", "mykey2": "myvalue2"})
self.keys.append(copied_key)
self.assertEqual(http.status_code, 200)


if __name__ == '__main__':
unittest.main()
11 changes: 8 additions & 3 deletions tests/unit/response_parsing/test_response_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def test_head_object(self):
'Content-Type': 'binary/octet-stream',
'accept-ranges': 'bytes',
'Last-Modified': 'Tue, 20 Aug 2013 18:33:25 GMT',
'x-amz-server-side-encryption': 'AES256'
'x-amz-server-side-encryption': 'AES256',
'x-amz-meta-mykey1': 'value1',
'x-amz-meta-mykey2': 'value2',
})
http_response.content = ''
http_response.request.method = 'HEAD'
Expand All @@ -185,8 +187,11 @@ def test_head_object(self):
"LastModified": "Tue, 20 Aug 2013 18:33:25 GMT",
"ContentLength": "265",
"ETag": '"40d06eb6194712ac1c915783004ef730"',
"ServerSideEncryption": "AES256"
}
"ServerSideEncryption": "AES256",
"Metadata": {
'mykey1': 'value1',
'mykey2': 'value2',
}}
response_data = get_response(self.session, put_object,
http_response)[1]
self.assertEqual(response_data, expected)
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_s3_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,23 @@ def test_put_object(self):
self.assertEqual(params['uri_params'], uri_params)
self.assertEqual(params['payload'].getvalue(), fp)

def test_copy_object(self):
operation = self.s3.get_operation('CopyObject')
bucket_name = 'mybucket'
key_name = 'key1'
copied_key= 'copied'
params = operation.build_parameters(
bucket=self.bucket_name, key=copied_key,
copy_source='%s/%s' % (bucket_name, key_name),
metadata_directive='REPLACE',
metadata={"mykey": "myvalue", "mykey2": "myvalue2"})
self.assertEqual(params['headers'], {
'x-amz-copy-source': 'mybucket/key1',
'x-amz-metadata-directive': 'REPLACE',
'x-amz-meta-mykey': 'myvalue',
'x-amz-meta-mykey2': 'myvalue2',
})

def test_complete_multipart_upload(self):
op = self.s3.get_operation('CompleteMultipartUpload')
parts = {
Expand Down