Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/s3api/test_put_object.py
1567 views
1
#!/usr/bin/env python
2
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License"). You
5
# may not use this file except in compliance with the License. A copy of
6
# the License is located at
7
#
8
# http://aws.amazon.com/apache2.0/
9
#
10
# or in the "license" file accompanying this file. This file is
11
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
12
# ANY KIND, either express or implied. See the License for the specific
13
# language governing permissions and limitations under the License.
14
import os
15
import re
16
import copy
17
18
from awscli.testutils import BaseAWSCommandParamsTest, FileCreator
19
20
import awscli.clidriver
21
22
# file is gone in python3, so instead IOBase must be used.
23
# Given this test module is the only place that cares about
24
# this type check, we do the check directly in this test module.
25
try:
26
file_type = file
27
except NameError:
28
import io
29
file_type = io.IOBase
30
31
32
class TestPutObject(BaseAWSCommandParamsTest):
33
34
maxDiff = None
35
prefix = 's3api put-object'
36
37
def setUp(self):
38
super(TestPutObject, self).setUp()
39
self.file_path = os.path.join(os.path.dirname(__file__),
40
'test_put_object_data')
41
self.files = FileCreator()
42
43
def tearDown(self):
44
super(TestPutObject, self).tearDown()
45
self.files.remove_all()
46
47
def test_simple(self):
48
cmdline = self.prefix
49
cmdline += ' --bucket mybucket'
50
cmdline += ' --key mykey'
51
cmdline += ' --body %s' % self.file_path
52
result = {'uri_params': {'Bucket': 'mybucket',
53
'Key': 'mykey'},
54
'headers': {'Expect': '100-continue'}}
55
expected = {
56
'Bucket': 'mybucket',
57
'Key': 'mykey'
58
}
59
self.assert_params_for_cmd(cmdline, expected, ignore_params=['Body'])
60
self.assertEqual(self.last_kwargs['Body'].name, self.file_path)
61
62
def test_headers(self):
63
cmdline = self.prefix
64
cmdline += ' --bucket mybucket'
65
cmdline += ' --key mykey'
66
cmdline += ' --body %s' % self.file_path
67
cmdline += ' --acl public-read'
68
cmdline += ' --content-encoding x-gzip'
69
cmdline += ' --content-type text/plain'
70
expected = {
71
'ACL': 'public-read',
72
'Bucket': 'mybucket',
73
'ContentEncoding': 'x-gzip',
74
'ContentType': 'text/plain',
75
'Key': 'mykey'
76
}
77
self.assert_params_for_cmd(cmdline, expected, ignore_params=['Body'])
78
self.assertEqual(self.last_kwargs['Body'].name, self.file_path)
79
80
def test_website_redirect(self):
81
cmdline = self.prefix
82
cmdline += ' --bucket mybucket'
83
cmdline += ' --key mykey'
84
cmdline += ' --acl public-read'
85
cmdline += ' --website-redirect-location http://www.example.com/'
86
expected = {
87
'ACL': 'public-read',
88
'Bucket': 'mybucket',
89
'Key': 'mykey',
90
'WebsiteRedirectLocation': 'http://www.example.com/'
91
}
92
self.assert_params_for_cmd(cmdline, expected)
93
94
def test_sse_key_with_binary_file(self):
95
# Create contents that do not get mapped to ascii
96
contents = b'\xc2'
97
filename = self.files.create_file('key', contents, mode='wb')
98
cmdline = self.prefix
99
cmdline += ' --bucket mybucket'
100
cmdline += ' --key mykey'
101
cmdline += ' --sse-customer-algorithm AES256'
102
cmdline += ' --sse-customer-key fileb://%s' % filename
103
expected = {
104
'Bucket': 'mybucket',
105
'Key': 'mykey',
106
'SSECustomerAlgorithm': 'AES256',
107
'SSECustomerKey': 'wg==', # Note the key gets base64 encoded.
108
'SSECustomerKeyMD5': 'ZGXa0dMXUr4/MoPo9w/u9w=='
109
}
110
self.assert_params_for_cmd(cmdline, expected)
111
112
113
if __name__ == "__main__":
114
unittest.main()
115
116