-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
test_s3.py
1186 lines (1005 loc) · 46.3 KB
/
test_s3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from tests import unittest, temporary_file, random_chars
import os
import time
from collections import defaultdict
import tempfile
import shutil
import threading
import logging
import mock
from tarfile import TarFile
from contextlib import closing
from nose.plugins.attrib import attr
from botocore.endpoint import Endpoint
from botocore.exceptions import ConnectionClosedError
from botocore.compat import six, zip_longest
import botocore.session
import botocore.auth
import botocore.credentials
import botocore.vendored.requests as requests
from botocore.config import Config
from botocore.exceptions import ClientError
def random_bucketname():
return 'botocoretest-' + random_chars(10)
LOG = logging.getLogger('botocore.tests.integration')
_SHARED_BUCKET = random_bucketname()
_DEFAULT_REGION = 'us-west-2'
def setup_module():
s3 = botocore.session.get_session().create_client('s3')
waiter = s3.get_waiter('bucket_exists')
params = {
'Bucket': _SHARED_BUCKET,
'CreateBucketConfiguration': {
'LocationConstraint': _DEFAULT_REGION,
}
}
try:
s3.create_bucket(**params)
except Exception as e:
# A create_bucket can fail for a number of reasons.
# We're going to defer to the waiter below to make the
# final call as to whether or not the bucket exists.
LOG.debug("create_bucket() raised an exception: %s", e, exc_info=True)
waiter.wait(Bucket=_SHARED_BUCKET)
def clear_out_bucket(bucket, region, delete_bucket=False):
s3 = botocore.session.get_session().create_client(
's3', region_name=region)
page = s3.get_paginator('list_objects')
# Use pages paired with batch delete_objects().
for page in page.paginate(Bucket=bucket):
keys = [{'Key': obj['Key']} for obj in page.get('Contents', [])]
if keys:
s3.delete_objects(Bucket=bucket, Delete={'Objects': keys})
if delete_bucket:
try:
s3.delete_bucket(Bucket=bucket)
except Exception as e:
# We can sometimes get exceptions when trying to
# delete a bucket. We'll let the waiter make
# the final call as to whether the bucket was able
# to be deleted.
LOG.debug("delete_bucket() raised an exception: %s",
e, exc_info=True)
waiter = s3.get_waiter('bucket_not_exists')
waiter.wait(Bucket=bucket)
def teardown_module():
clear_out_bucket(_SHARED_BUCKET, _DEFAULT_REGION, delete_bucket=True)
class BaseS3ClientTest(unittest.TestCase):
def setUp(self):
self.bucket_name = _SHARED_BUCKET
self.region = _DEFAULT_REGION
clear_out_bucket(self.bucket_name, self.region)
self.session = botocore.session.get_session()
self.client = self.session.create_client('s3', region_name=self.region)
def assert_status_code(self, response, status_code):
self.assertEqual(
response['ResponseMetadata']['HTTPStatusCode'],
status_code
)
def create_bucket(self, region_name, bucket_name=None, client=None):
bucket_client = client or self.client
if bucket_name is None:
bucket_name = random_bucketname()
bucket_kwargs = {'Bucket': bucket_name}
if region_name != 'us-east-1':
bucket_kwargs['CreateBucketConfiguration'] = {
'LocationConstraint': region_name,
}
response = bucket_client.create_bucket(**bucket_kwargs)
self.assert_status_code(response, 200)
waiter = bucket_client.get_waiter('bucket_exists')
waiter.wait(Bucket=bucket_name)
self.addCleanup(clear_out_bucket, bucket_name, region_name, True)
return bucket_name
def make_tempdir(self):
tempdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tempdir)
return tempdir
class TestS3BaseWithBucket(BaseS3ClientTest):
def setUp(self):
super(TestS3BaseWithBucket, self).setUp()
self.caught_exceptions = []
def create_object(self, key_name, body='foo'):
self.client.put_object(
Bucket=self.bucket_name, Key=key_name,
Body=body)
def create_multipart_upload(self, key_name):
parsed = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=key_name)
upload_id = parsed['UploadId']
self.addCleanup(
self.client.abort_multipart_upload,
UploadId=upload_id,
Bucket=self.bucket_name, Key=key_name)
def abort_multipart_upload(self, bucket_name, key, upload_id):
self.client.abort_multipart_upload(
UploadId=upload_id, Bucket=self.bucket_name, Key=key)
def delete_object(self, key, bucket_name):
response = self.client.delete_object(Bucket=bucket_name, Key=key)
self.assert_status_code(response, 204)
def delete_bucket(self, bucket_name):
response = self.client.delete_bucket(Bucket=bucket_name)
self.assert_status_code(response, 204)
def create_object_catch_exceptions(self, key_name):
try:
self.create_object(key_name=key_name)
except Exception as e:
self.caught_exceptions.append(e)
def assert_num_uploads_found(self, operation, num_uploads,
max_items=None, num_attempts=5):
amount_seen = None
paginator = self.client.get_paginator(operation)
for _ in range(num_attempts):
pages = paginator.paginate(Bucket=self.bucket_name,
PaginationConfig={
'MaxItems': max_items})
iterators = pages.result_key_iters()
self.assertEqual(len(iterators), 2)
self.assertEqual(iterators[0].result_key.expression, 'Uploads')
# It sometimes takes a while for all the uploads to show up,
# especially if the upload was just created. If we don't
# see the expected amount, we retry up to num_attempts time
# before failing.
amount_seen = len(list(iterators[0]))
if amount_seen == num_uploads:
# Test passed.
return
else:
# Sleep and try again.
time.sleep(2)
self.fail("Expected to see %s uploads, instead saw: %s" % (
num_uploads, amount_seen))
def create_client(self):
# Even though the default signature_version is s3,
# we're being explicit in case this ever changes.
client_config = Config(signature_version='s3')
return self.session.create_client('s3', self.region,
config=client_config)
def assert_can_put_object(self, body):
client = self.create_client()
response = client.put_object(
Bucket=self.bucket_name, Key='foo',
Body=body)
self.assert_status_code(response, 200)
self.addCleanup(
client.delete_object, Bucket=self.bucket_name, Key='foo')
class TestS3Buckets(TestS3BaseWithBucket):
def setUp(self):
super(TestS3Buckets, self).setUp()
def test_can_make_request(self):
# Basic smoke test to ensure we can talk to s3.
result = self.client.list_buckets()
# Can't really assume anything about whether or not they have buckets,
# but we can assume something about the structure of the response.
self.assertEqual(sorted(list(result.keys())),
['Buckets', 'Owner', 'ResponseMetadata'])
def test_can_get_bucket_location(self):
result = self.client.get_bucket_location(Bucket=self.bucket_name)
self.assertIn('LocationConstraint', result)
self.assertEqual(result['LocationConstraint'], self.region)
class TestS3Objects(TestS3BaseWithBucket):
def increment_auth(self, request, **kwargs):
self.auth_paths.append(request.auth_path)
def test_can_delete_urlencoded_object(self):
key_name = 'a+b/foo'
self.create_object(key_name=key_name)
bucket_contents = self.client.list_objects(
Bucket=self.bucket_name)['Contents']
self.assertEqual(len(bucket_contents), 1)
self.assertEqual(bucket_contents[0]['Key'], 'a+b/foo')
subdir_contents = self.client.list_objects(
Bucket=self.bucket_name, Prefix='a+b')['Contents']
self.assertEqual(len(subdir_contents), 1)
self.assertEqual(subdir_contents[0]['Key'], 'a+b/foo')
response = self.client.delete_object(
Bucket=self.bucket_name, Key=key_name)
self.assert_status_code(response, 204)
@attr('slow')
def test_can_paginate(self):
for i in range(5):
key_name = 'key%s' % i
self.create_object(key_name)
# Eventual consistency.
time.sleep(3)
paginator = self.client.get_paginator('list_objects')
generator = paginator.paginate(MaxKeys=1,
Bucket=self.bucket_name)
responses = list(generator)
self.assertEqual(len(responses), 5, responses)
key_names = [el['Contents'][0]['Key']
for el in responses]
self.assertEqual(key_names, ['key0', 'key1', 'key2', 'key3', 'key4'])
@attr('slow')
def test_can_paginate_with_page_size(self):
for i in range(5):
key_name = 'key%s' % i
self.create_object(key_name)
# Eventual consistency.
time.sleep(3)
paginator = self.client.get_paginator('list_objects')
generator = paginator.paginate(PaginationConfig={'PageSize': 1},
Bucket=self.bucket_name)
responses = list(generator)
self.assertEqual(len(responses), 5, responses)
data = [r for r in responses]
key_names = [el['Contents'][0]['Key']
for el in data]
self.assertEqual(key_names, ['key0', 'key1', 'key2', 'key3', 'key4'])
@attr('slow')
def test_result_key_iters(self):
for i in range(5):
key_name = 'key/%s/%s' % (i, i)
self.create_object(key_name)
key_name2 = 'key/%s' % i
self.create_object(key_name2)
time.sleep(3)
paginator = self.client.get_paginator('list_objects')
generator = paginator.paginate(MaxKeys=2,
Prefix='key/',
Delimiter='/',
Bucket=self.bucket_name)
iterators = generator.result_key_iters()
response = defaultdict(list)
key_names = [i.result_key for i in iterators]
for vals in zip_longest(*iterators):
for k, val in zip(key_names, vals):
response.setdefault(k.expression, [])
response[k.expression].append(val)
self.assertIn('Contents', response)
self.assertIn('CommonPrefixes', response)
@attr('slow')
def test_can_get_and_put_object(self):
self.create_object('foobarbaz', body='body contents')
time.sleep(3)
data = self.client.get_object(
Bucket=self.bucket_name, Key='foobarbaz')
self.assertEqual(data['Body'].read().decode('utf-8'), 'body contents')
def test_can_put_large_string_body_on_new_bucket(self):
body = '*' * (5 * (1024 ** 2))
self.assert_can_put_object(body)
def test_can_put_object_bytearray(self):
body_bytes = b'*' * 1024
body = bytearray(body_bytes)
self.assert_can_put_object(body)
def test_get_object_stream_wrapper(self):
self.create_object('foobarbaz', body='body contents')
response = self.client.get_object(
Bucket=self.bucket_name, Key='foobarbaz')
body = response['Body']
# Am able to set a socket timeout
body.set_socket_timeout(10)
self.assertEqual(body.read(amt=1).decode('utf-8'), 'b')
self.assertEqual(body.read().decode('utf-8'), 'ody contents')
def test_paginate_max_items(self):
self.create_multipart_upload('foo/key1')
self.create_multipart_upload('foo/key1')
self.create_multipart_upload('foo/key1')
self.create_multipart_upload('foo/key2')
self.create_multipart_upload('foobar/key1')
self.create_multipart_upload('foobar/key2')
self.create_multipart_upload('bar/key1')
self.create_multipart_upload('bar/key2')
# Verify when we have MaxItems=None, we get back all 8 uploads.
self.assert_num_uploads_found('list_multipart_uploads',
max_items=None, num_uploads=8)
# Verify when we have MaxItems=1, we get back 1 upload.
self.assert_num_uploads_found('list_multipart_uploads',
max_items=1, num_uploads=1)
paginator = self.client.get_paginator('list_multipart_uploads')
# Works similar with build_full_result()
pages = paginator.paginate(PaginationConfig={'MaxItems': 1},
Bucket=self.bucket_name)
full_result = pages.build_full_result()
self.assertEqual(len(full_result['Uploads']), 1)
def test_paginate_within_page_boundaries(self):
self.create_object('a')
self.create_object('b')
self.create_object('c')
self.create_object('d')
paginator = self.client.get_paginator('list_objects')
# First do it without a max keys so we're operating on a single page of
# results.
pages = paginator.paginate(PaginationConfig={'MaxItems': 1},
Bucket=self.bucket_name)
first = pages.build_full_result()
t1 = first['NextToken']
pages = paginator.paginate(
PaginationConfig={'MaxItems': 1, 'StartingToken': t1},
Bucket=self.bucket_name)
second = pages.build_full_result()
t2 = second['NextToken']
pages = paginator.paginate(
PaginationConfig={'MaxItems': 1, 'StartingToken': t2},
Bucket=self.bucket_name)
third = pages.build_full_result()
t3 = third['NextToken']
pages = paginator.paginate(
PaginationConfig={'MaxItems': 1, 'StartingToken': t3},
Bucket=self.bucket_name)
fourth = pages.build_full_result()
self.assertEqual(first['Contents'][-1]['Key'], 'a')
self.assertEqual(second['Contents'][-1]['Key'], 'b')
self.assertEqual(third['Contents'][-1]['Key'], 'c')
self.assertEqual(fourth['Contents'][-1]['Key'], 'd')
def test_unicode_key_put_list(self):
# Verify we can upload a key with a unicode char and list it as well.
key_name = u'\u2713'
self.create_object(key_name)
parsed = self.client.list_objects(Bucket=self.bucket_name)
self.assertEqual(len(parsed['Contents']), 1)
self.assertEqual(parsed['Contents'][0]['Key'], key_name)
parsed = self.client.get_object(
Bucket=self.bucket_name, Key=key_name)
self.assertEqual(parsed['Body'].read().decode('utf-8'), 'foo')
def test_unicode_system_character(self):
# Verify we can use a unicode system character which would normally
# break the xml parser
key_name = 'foo\x08'
self.create_object(key_name)
self.addCleanup(self.delete_object, key_name, self.bucket_name)
parsed = self.client.list_objects(Bucket=self.bucket_name)
self.assertEqual(len(parsed['Contents']), 1)
self.assertEqual(parsed['Contents'][0]['Key'], key_name)
parsed = self.client.list_objects(Bucket=self.bucket_name,
EncodingType='url')
self.assertEqual(len(parsed['Contents']), 1)
self.assertEqual(parsed['Contents'][0]['Key'], 'foo%08')
def test_thread_safe_auth(self):
self.auth_paths = []
self.session.register('before-sign', self.increment_auth)
# This test depends on auth_path, which is only added in virtual host
# style requests.
config = Config(s3={'addressing_style': 'virtual'})
self.client = self.session.create_client('s3', self.region,
config=config)
self.create_object(key_name='foo1')
threads = []
for i in range(10):
t = threading.Thread(target=self.create_object_catch_exceptions,
args=('foo%s' % i,))
t.daemon = True
threads.append(t)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
self.assertEqual(
self.caught_exceptions, [],
"Unexpectedly caught exceptions: %s" % self.caught_exceptions)
self.assertEqual(
len(set(self.auth_paths)), 10,
"Expected 10 unique auth paths, instead received: %s" %
(self.auth_paths))
def test_non_normalized_key_paths(self):
# The create_object method has assertEqual checks for 200 status.
self.create_object('key./././name')
bucket_contents = self.client.list_objects(
Bucket=self.bucket_name)['Contents']
self.assertEqual(len(bucket_contents), 1)
self.assertEqual(bucket_contents[0]['Key'], 'key./././name')
class TestS3Regions(BaseS3ClientTest):
def setUp(self):
super(TestS3Regions, self).setUp()
self.region = 'us-west-2'
self.client = self.session.create_client(
's3', region_name=self.region)
def test_reset_stream_on_redirects(self):
# Create a bucket in a non classic region.
bucket_name = self.create_bucket(self.region)
# Then try to put a file like object to this location.
tempdir = self.make_tempdir()
filename = os.path.join(tempdir, 'foo')
with open(filename, 'wb') as f:
f.write(b'foo' * 1024)
with open(filename, 'rb') as f:
self.client.put_object(
Bucket=bucket_name, Key='foo', Body=f)
data = self.client.get_object(
Bucket=bucket_name, Key='foo')
self.assertEqual(data['Body'].read(), b'foo' * 1024)
class TestS3Copy(TestS3BaseWithBucket):
def test_copy_with_quoted_char(self):
key_name = 'a+b/foo'
self.create_object(key_name=key_name)
key_name2 = key_name + 'bar'
self.client.copy_object(
Bucket=self.bucket_name, Key=key_name2,
CopySource='%s/%s' % (self.bucket_name, key_name))
# Now verify we can retrieve the copied object.
data = self.client.get_object(
Bucket=self.bucket_name, Key=key_name2)
self.assertEqual(data['Body'].read().decode('utf-8'), 'foo')
def test_copy_with_query_string(self):
key_name = 'a+b/foo?notVersionid=bar'
self.create_object(key_name=key_name)
key_name2 = key_name + 'bar'
self.client.copy_object(
Bucket=self.bucket_name, Key=key_name2,
CopySource='%s/%s' % (self.bucket_name, key_name))
# Now verify we can retrieve the copied object.
data = self.client.get_object(
Bucket=self.bucket_name, Key=key_name2)
self.assertEqual(data['Body'].read().decode('utf-8'), 'foo')
def test_can_copy_with_dict_form(self):
key_name = 'a+b/foo?versionId=abcd'
self.create_object(key_name=key_name)
key_name2 = key_name + 'bar'
self.client.copy_object(
Bucket=self.bucket_name, Key=key_name2,
CopySource={'Bucket': self.bucket_name,
'Key': key_name})
# Now verify we can retrieve the copied object.
data = self.client.get_object(
Bucket=self.bucket_name, Key=key_name2)
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'
parsed = self.client.copy_object(
Bucket=self.bucket_name, Key=copied_key,
CopySource='%s/%s' % (self.bucket_name, key_name),
MetadataDirective='REPLACE',
Metadata={"mykey": "myvalue", "mykey2": "myvalue2"})
self.assert_status_code(parsed, 200)
class BaseS3PresignTest(BaseS3ClientTest):
def setup_bucket(self):
self.key = 'myobject'
self.create_object(key_name=self.key)
def create_object(self, key_name, body='foo'):
self.client.put_object(
Bucket=self.bucket_name, Key=key_name,
Body=body)
class TestS3PresignUsStandard(BaseS3PresignTest):
def setUp(self):
super(TestS3PresignUsStandard, self).setUp()
self.region = 'us-east-1'
self.client_config = Config(
region_name=self.region, signature_version='s3')
self.client = self.session.create_client(
's3', config=self.client_config)
self.bucket_name = self.create_bucket(self.region)
self.setup_bucket()
def test_presign_sigv2(self):
presigned_url = self.client.generate_presigned_url(
'get_object', Params={'Bucket': self.bucket_name, 'Key': self.key})
self.assertTrue(
presigned_url.startswith(
'https://%s.s3.amazonaws.com/%s' % (
self.bucket_name, self.key)),
"Host was suppose to use DNS style, instead "
"got: %s" % presigned_url)
# Try to retrieve the object using the presigned url.
self.assertEqual(requests.get(presigned_url).content, b'foo')
def test_presign_with_existing_query_string_values(self):
content_disposition = 'attachment; filename=foo.txt;'
presigned_url = self.client.generate_presigned_url(
'get_object', Params={
'Bucket': self.bucket_name, 'Key': self.key,
'ResponseContentDisposition': content_disposition})
response = requests.get(presigned_url)
self.assertEqual(response.headers['Content-Disposition'],
content_disposition)
self.assertEqual(response.content, b'foo')
def test_presign_sigv4(self):
self.client_config.signature_version = 's3v4'
self.client = self.session.create_client(
's3', config=self.client_config)
presigned_url = self.client.generate_presigned_url(
'get_object', Params={'Bucket': self.bucket_name, 'Key': self.key})
self.assertTrue(
presigned_url.startswith(
'https://%s.s3.amazonaws.com/%s' % (
self.bucket_name, self.key)),
"Host was suppose to be the us-east-1 endpoint, instead "
"got: %s" % presigned_url)
# Try to retrieve the object using the presigned url.
self.assertEqual(requests.get(presigned_url).content, b'foo')
def test_presign_post_sigv2(self):
# Create some of the various supported conditions.
conditions = [
{"acl": "public-read"},
]
# Create the fields that follow the policy.
fields = {
'acl': 'public-read',
}
# Retrieve the args for the presigned post.
post_args = self.client.generate_presigned_post(
self.bucket_name, self.key, Fields=fields,
Conditions=conditions)
# Make sure that the form can be posted successfully.
files = {'file': ('baz', 'some data')}
# Make sure the correct endpoint is being used
self.assertTrue(
post_args['url'].startswith(
'https://%s.s3.amazonaws.com' % self.bucket_name),
"Host was suppose to use DNS style, instead "
"got: %s" % post_args['url'])
# Try to retrieve the object using the presigned url.
r = requests.post(
post_args['url'], data=post_args['fields'], files=files)
self.assertEqual(r.status_code, 204)
def test_presign_post_sigv4(self):
self.client_config.signature_version = 's3v4'
self.client = self.session.create_client(
's3', config=self.client_config)
# Create some of the various supported conditions.
conditions = [
{"acl": 'public-read'},
]
# Create the fields that follow the policy.
fields = {
'acl': 'public-read',
}
# Retrieve the args for the presigned post.
post_args = self.client.generate_presigned_post(
self.bucket_name, self.key, Fields=fields,
Conditions=conditions)
# Make sure that the form can be posted successfully.
files = {'file': ('baz', 'some data')}
# Make sure the correct endpoint is being used
self.assertTrue(
post_args['url'].startswith(
'https://%s.s3.amazonaws.com/' % self.bucket_name),
"Host was suppose to use us-east-1 endpoint, instead "
"got: %s" % post_args['url'])
r = requests.post(
post_args['url'], data=post_args['fields'], files=files)
self.assertEqual(r.status_code, 204)
class TestS3PresignNonUsStandard(BaseS3PresignTest):
def setUp(self):
super(TestS3PresignNonUsStandard, self).setUp()
self.client_config = Config(
region_name=self.region, signature_version='s3')
self.client = self.session.create_client(
's3', config=self.client_config)
self.setup_bucket()
def test_presign_sigv2(self):
presigned_url = self.client.generate_presigned_url(
'get_object', Params={'Bucket': self.bucket_name, 'Key': self.key})
self.assertTrue(
presigned_url.startswith(
'https://%s.s3.amazonaws.com/%s' % (
self.bucket_name, self.key)),
"Host was suppose to use DNS style, instead "
"got: %s" % presigned_url)
# Try to retrieve the object using the presigned url.
self.assertEqual(requests.get(presigned_url).content, b'foo')
def test_presign_sigv4(self):
# For a newly created bucket, you can't use virtualhosted
# addressing and 's3v4' due to the backwards compat behavior
# using '.s3.amazonaws.com' for anything in the AWS partition.
# Instead you either have to use the older 's3' signature version
# of you have to use path style addressing. The latter is being
# done here.
self.client_config.signature_version = 's3v4'
self.client_config.s3 = {'addressing_style': 'path'}
self.client = self.session.create_client(
's3', config=self.client_config)
presigned_url = self.client.generate_presigned_url(
'get_object', Params={'Bucket': self.bucket_name, 'Key': self.key})
self.assertTrue(
presigned_url.startswith(
'https://s3.us-west-2.amazonaws.com/%s/%s' % (
self.bucket_name, self.key)),
"Host was suppose to be the us-west-2 endpoint, instead "
"got: %s" % presigned_url)
# Try to retrieve the object using the presigned url.
self.assertEqual(requests.get(presigned_url).content, b'foo')
def test_presign_post_sigv2(self):
# Create some of the various supported conditions.
conditions = [
{"acl": "public-read"},
]
# Create the fields that follow the policy.
fields = {
'acl': 'public-read',
}
# Retrieve the args for the presigned post.
post_args = self.client.generate_presigned_post(
self.bucket_name, self.key, Fields=fields, Conditions=conditions)
# Make sure that the form can be posted successfully.
files = {'file': ('baz', 'some data')}
# Make sure the correct endpoint is being used
self.assertTrue(
post_args['url'].startswith(
'https://%s.s3.amazonaws.com' % self.bucket_name),
"Host was suppose to use DNS style, instead "
"got: %s" % post_args['url'])
r = requests.post(
post_args['url'], data=post_args['fields'], files=files)
self.assertEqual(r.status_code, 204)
def test_presign_post_sigv4(self):
self.client_config.signature_version = 's3v4'
self.client = self.session.create_client(
's3', config=self.client_config)
# Create some of the various supported conditions.
conditions = [
{"acl": "public-read"},
]
# Create the fields that follow the policy.
fields = {
'acl': 'public-read',
}
# Retrieve the args for the presigned post.
post_args = self.client.generate_presigned_post(
self.bucket_name, self.key, Fields=fields, Conditions=conditions)
# Make sure that the form can be posted successfully.
files = {'file': ('baz', 'some data')}
# Make sure the correct endpoint is being used
self.assertTrue(
post_args['url'].startswith(
'https://%s.s3.amazonaws.com/' % self.bucket_name),
"Host was suppose to use DNS style, instead "
"got: %s" % post_args['url'])
r = requests.post(
post_args['url'], data=post_args['fields'], files=files)
self.assertEqual(r.status_code, 204)
class TestCreateBucketInOtherRegion(TestS3BaseWithBucket):
def test_bucket_in_other_region(self):
# This verifies expect 100-continue behavior. We previously
# had a bug where we did not support this behavior and trying to
# create a bucket and immediately PutObject with a file like object
# would actually cause errors.
client = self.session.create_client('s3', 'us-east-1')
with temporary_file('w') as f:
f.write('foobarbaz' * 1024 * 1024)
f.flush()
with open(f.name, 'rb') as body_file:
response = client.put_object(
Bucket=self.bucket_name,
Key='foo.txt', Body=body_file)
self.assert_status_code(response, 200)
def test_bucket_in_other_region_using_http(self):
client = self.session.create_client(
's3', 'us-east-1', endpoint_url='http://s3.amazonaws.com/')
with temporary_file('w') as f:
f.write('foobarbaz' * 1024 * 1024)
f.flush()
with open(f.name, 'rb') as body_file:
response = client.put_object(
Bucket=self.bucket_name,
Key='foo.txt', Body=body_file)
self.assert_status_code(response, 200)
class TestS3SigV4Client(BaseS3ClientTest):
def setUp(self):
super(TestS3SigV4Client, self).setUp()
self.client = self.session.create_client(
's3', self.region, config=Config(signature_version='s3v4'))
def test_can_get_bucket_location(self):
# Even though the bucket is in us-west-2, we should still be able to
# use the us-east-1 endpoint class to get the bucket location.
client = self.session.create_client('s3', 'us-east-1')
# Also keep in mind that while this test is useful, it doesn't test
# what happens once DNS propogates which is arguably more interesting,
# as DNS will point us to the eu-central-1 endpoint.
response = client.get_bucket_location(Bucket=self.bucket_name)
self.assertEqual(response['LocationConstraint'], 'us-west-2')
def test_request_retried_for_sigv4(self):
body = six.BytesIO(b"Hello world!")
original_send = Endpoint._send
state = mock.Mock()
state.error_raised = False
def mock_endpoint_send(self, *args, **kwargs):
if not state.error_raised:
state.error_raised = True
raise ConnectionClosedError(endpoint_url='')
else:
return original_send(self, *args, **kwargs)
# TODO: fix with stubber / before send event
with mock.patch('botocore.endpoint.Endpoint._send', mock_endpoint_send):
response = self.client.put_object(Bucket=self.bucket_name,
Key='foo.txt', Body=body)
self.assert_status_code(response, 200)
@attr('slow')
def test_paginate_list_objects_unicode(self):
key_names = [
u'non-ascii-key-\xe4\xf6\xfc-01.txt',
u'non-ascii-key-\xe4\xf6\xfc-02.txt',
u'non-ascii-key-\xe4\xf6\xfc-03.txt',
u'non-ascii-key-\xe4\xf6\xfc-04.txt',
]
for key in key_names:
response = self.client.put_object(Bucket=self.bucket_name,
Key=key, Body='')
self.assert_status_code(response, 200)
list_objs_paginator = self.client.get_paginator('list_objects')
key_refs = []
for response in list_objs_paginator.paginate(Bucket=self.bucket_name,
PaginationConfig={
'PageSize': 2}):
for content in response['Contents']:
key_refs.append(content['Key'])
self.assertEqual(key_names, key_refs)
@attr('slow')
def test_paginate_list_objects_safe_chars(self):
key_names = [
u'-._~safe-chars-key-01.txt',
u'-._~safe-chars-key-02.txt',
u'-._~safe-chars-key-03.txt',
u'-._~safe-chars-key-04.txt',
]
for key in key_names:
response = self.client.put_object(Bucket=self.bucket_name,
Key=key, Body='')
self.assert_status_code(response, 200)
list_objs_paginator = self.client.get_paginator('list_objects')
key_refs = []
for response in list_objs_paginator.paginate(Bucket=self.bucket_name,
PaginationConfig={
'PageSize': 2}):
for content in response['Contents']:
key_refs.append(content['Key'])
self.assertEqual(key_names, key_refs)
def test_create_multipart_upload(self):
key = 'mymultipartupload'
response = self.client.create_multipart_upload(
Bucket=self.bucket_name, Key=key
)
self.assert_status_code(response, 200)
upload_id = response['UploadId']
self.addCleanup(
self.client.abort_multipart_upload,
Bucket=self.bucket_name, Key=key, UploadId=upload_id
)
response = self.client.list_multipart_uploads(
Bucket=self.bucket_name, Prefix=key
)
# Make sure there is only one multipart upload.
self.assertEqual(len(response['Uploads']), 1)
# Make sure the upload id is as expected.
self.assertEqual(response['Uploads'][0]['UploadId'], upload_id)
def test_can_add_double_space_metadata(self):
# Ensure we get no sigv4 errors when we send
# metadata with consecutive spaces.
response = self.client.put_object(
Bucket=self.bucket_name, Key='foo.txt',
Body=b'foobar', Metadata={'foo': ' multi spaces '})
self.assert_status_code(response, 200)
def test_bad_request_on_invalid_credentials(self):
# A previous bug would cause this to hang. We want
# to verify we get the 400 response.
# In order to test we need a key that actually
# exists so we use the properly configured self.client.
self.client.put_object(Bucket=self.bucket_name,
Key='foo.txt',
Body=b'asdfasdf')
# Now we create a client with a bad session token
# which should give us a 400 response.
creds = self.session.get_credentials()
client = self.session.create_client(
's3', self.region,
config=Config(signature_version='s3v4'),
aws_access_key_id=creds.access_key,
aws_secret_access_key=creds.secret_key,
aws_session_token='bad-token-causes-400',
)
with self.assertRaises(ClientError) as e:
client.head_object(
Bucket=self.bucket_name,
Key='foo.txt',
)
self.assertEqual(e.exception.response['Error']['Code'], '400')
class TestSSEKeyParamValidation(BaseS3ClientTest):
def test_make_request_with_sse(self):
key_bytes = os.urandom(32)
# Obviously a bad key here, but we just want to ensure we can use
# a str/unicode type as a key.
key_str = 'abcd' * 8
# Put two objects with an sse key, one with random bytes,
# one with str/unicode. Then verify we can GetObject() both
# objects.
self.client.put_object(
Bucket=self.bucket_name, Key='foo.txt',
Body=six.BytesIO(b'mycontents'), SSECustomerAlgorithm='AES256',
SSECustomerKey=key_bytes)
self.addCleanup(self.client.delete_object,
Bucket=self.bucket_name, Key='foo.txt')
self.client.put_object(
Bucket=self.bucket_name, Key='foo2.txt',
Body=six.BytesIO(b'mycontents2'), SSECustomerAlgorithm='AES256',
SSECustomerKey=key_str)
self.addCleanup(self.client.delete_object,
Bucket=self.bucket_name, Key='foo2.txt')
self.assertEqual(
self.client.get_object(Bucket=self.bucket_name,
Key='foo.txt',
SSECustomerAlgorithm='AES256',
SSECustomerKey=key_bytes)['Body'].read(),
b'mycontents')
self.assertEqual(
self.client.get_object(Bucket=self.bucket_name,
Key='foo2.txt',
SSECustomerAlgorithm='AES256',
SSECustomerKey=key_str)['Body'].read(),
b'mycontents2')
def test_make_request_with_sse_copy_source(self):
encrypt_key = 'a' * 32
other_encrypt_key = 'b' * 32
# Upload the object using one encrypt key
self.client.put_object(
Bucket=self.bucket_name, Key='foo.txt',
Body=six.BytesIO(b'mycontents'), SSECustomerAlgorithm='AES256',
SSECustomerKey=encrypt_key)
self.addCleanup(self.client.delete_object,
Bucket=self.bucket_name, Key='foo.txt')
# Copy the object using the original encryption key as the copy source
# and encrypt with a new encryption key.
self.client.copy_object(
Bucket=self.bucket_name,
CopySource=self.bucket_name+'/foo.txt',
Key='bar.txt', CopySourceSSECustomerAlgorithm='AES256',
CopySourceSSECustomerKey=encrypt_key,
SSECustomerAlgorithm='AES256',
SSECustomerKey=other_encrypt_key
)
self.addCleanup(self.client.delete_object,
Bucket=self.bucket_name, Key='bar.txt')
# Download the object using the new encryption key.
# The content should not have changed.