Path: blob/develop/tests/functional/s3api/test_put_object.py
1567 views
#!/usr/bin/env python1# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13import os14import re15import copy1617from awscli.testutils import BaseAWSCommandParamsTest, FileCreator1819import awscli.clidriver2021# file is gone in python3, so instead IOBase must be used.22# Given this test module is the only place that cares about23# this type check, we do the check directly in this test module.24try:25file_type = file26except NameError:27import io28file_type = io.IOBase293031class TestPutObject(BaseAWSCommandParamsTest):3233maxDiff = None34prefix = 's3api put-object'3536def setUp(self):37super(TestPutObject, self).setUp()38self.file_path = os.path.join(os.path.dirname(__file__),39'test_put_object_data')40self.files = FileCreator()4142def tearDown(self):43super(TestPutObject, self).tearDown()44self.files.remove_all()4546def test_simple(self):47cmdline = self.prefix48cmdline += ' --bucket mybucket'49cmdline += ' --key mykey'50cmdline += ' --body %s' % self.file_path51result = {'uri_params': {'Bucket': 'mybucket',52'Key': 'mykey'},53'headers': {'Expect': '100-continue'}}54expected = {55'Bucket': 'mybucket',56'Key': 'mykey'57}58self.assert_params_for_cmd(cmdline, expected, ignore_params=['Body'])59self.assertEqual(self.last_kwargs['Body'].name, self.file_path)6061def test_headers(self):62cmdline = self.prefix63cmdline += ' --bucket mybucket'64cmdline += ' --key mykey'65cmdline += ' --body %s' % self.file_path66cmdline += ' --acl public-read'67cmdline += ' --content-encoding x-gzip'68cmdline += ' --content-type text/plain'69expected = {70'ACL': 'public-read',71'Bucket': 'mybucket',72'ContentEncoding': 'x-gzip',73'ContentType': 'text/plain',74'Key': 'mykey'75}76self.assert_params_for_cmd(cmdline, expected, ignore_params=['Body'])77self.assertEqual(self.last_kwargs['Body'].name, self.file_path)7879def test_website_redirect(self):80cmdline = self.prefix81cmdline += ' --bucket mybucket'82cmdline += ' --key mykey'83cmdline += ' --acl public-read'84cmdline += ' --website-redirect-location http://www.example.com/'85expected = {86'ACL': 'public-read',87'Bucket': 'mybucket',88'Key': 'mykey',89'WebsiteRedirectLocation': 'http://www.example.com/'90}91self.assert_params_for_cmd(cmdline, expected)9293def test_sse_key_with_binary_file(self):94# Create contents that do not get mapped to ascii95contents = b'\xc2'96filename = self.files.create_file('key', contents, mode='wb')97cmdline = self.prefix98cmdline += ' --bucket mybucket'99cmdline += ' --key mykey'100cmdline += ' --sse-customer-algorithm AES256'101cmdline += ' --sse-customer-key fileb://%s' % filename102expected = {103'Bucket': 'mybucket',104'Key': 'mykey',105'SSECustomerAlgorithm': 'AES256',106'SSECustomerKey': 'wg==', # Note the key gets base64 encoded.107'SSECustomerKeyMD5': 'ZGXa0dMXUr4/MoPo9w/u9w=='108}109self.assert_params_for_cmd(cmdline, expected)110111112if __name__ == "__main__":113unittest.main()114115116