File: test_s3.py

package info (click to toggle)
python-django-storages 1.14.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 896 kB
  • sloc: python: 4,548; makefile: 119; sh: 6
file content (1217 lines) | stat: -rw-r--r-- 44,320 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
import datetime
import gzip
import io
import os
import pickle
import threading
from textwrap import dedent
from unittest import mock
from unittest import skipIf
from urllib.parse import urlparse

try:
    import moto
except ImportError:
    import unittest
    raise unittest.SkipTest("moto package not installed, skipping tests")


import boto3
import boto3.s3.transfer
import botocore
from botocore.config import Config as ClientConfig
from botocore.exceptions import ClientError
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import ContentFile
from django.core.files.base import File
from django.test import TestCase
from django.test import override_settings
from django.utils.timezone import is_aware
from moto import mock_s3

from storages.backends import s3
from tests.utils import NonSeekableContentFile


class S3ManifestStaticStorageTestStorage(s3.S3ManifestStaticStorage):
    def read_manifest(self):
        return None


class S3StorageTests(TestCase):
    def setUp(self):
        self.storage = s3.S3Storage()
        self.storage._connections.connection = mock.MagicMock()
        self.storage._unsigned_connections.connection = mock.MagicMock()

    @mock.patch("boto3.Session")
    def test_s3_session(self, session):
        with override_settings(AWS_S3_SESSION_PROFILE="test_profile"):
            storage = s3.S3Storage()
            _ = storage.connection
            session.assert_called_once_with(profile_name="test_profile")

    @mock.patch("boto3.Session.resource")
    def test_client_config(self, resource):
        with override_settings(
            AWS_S3_CLIENT_CONFIG=ClientConfig(max_pool_connections=30)
        ):
            storage = s3.S3Storage()
            _ = storage.connection
            resource.assert_called_once()
            self.assertEqual(30, resource.call_args[1]["config"].max_pool_connections)

    @mock.patch("boto3.Session.resource")
    def test_connection_unsiged(self, resource):
        with override_settings(AWS_S3_ADDRESSING_STYLE="virtual"):
            storage = s3.S3Storage()
            _ = storage.unsigned_connection
            resource.assert_called_once()
            self.assertEqual(
                botocore.UNSIGNED, resource.call_args[1]["config"].signature_version
            )
            self.assertEqual(
                "virtual", resource.call_args[1]["config"].s3["addressing_style"]
            )

    def test_pickle_with_bucket(self):
        """
        Test that the storage can be pickled with a bucket attached
        """
        # Ensure the bucket has been used
        self.storage.bucket
        self.assertIsNotNone(self.storage._bucket)

        # Can't pickle MagicMock, but you can't pickle a real Bucket object either
        p = pickle.dumps(self.storage)
        new_storage = pickle.loads(p)

        self.assertIsInstance(new_storage._connections, threading.local)
        # Put the mock connection back in
        new_storage._connections.connection = mock.MagicMock()

        self.assertIsNone(new_storage._bucket)
        new_storage.bucket
        self.assertIsNotNone(new_storage._bucket)

    def test_pickle_without_bucket(self):
        """
        Test that the storage can be pickled, without a bucket instance
        """

        # Can't pickle a threadlocal
        p = pickle.dumps(self.storage)
        new_storage = pickle.loads(p)

        self.assertIsInstance(new_storage._connections, threading.local)

    def test_storage_url_slashes(self):
        """
        Test URL generation.
        """
        self.storage.custom_domain = "example.com"

        # We expect no leading slashes in the path,
        # and trailing slashes should be preserved.
        self.assertEqual(self.storage.url(""), "https://example.com/")
        self.assertEqual(self.storage.url("path"), "https://example.com/path")
        self.assertEqual(self.storage.url("path/"), "https://example.com/path/")
        self.assertEqual(self.storage.url("path/1"), "https://example.com/path/1")
        self.assertEqual(self.storage.url("path/1/"), "https://example.com/path/1/")

    def test_storage_save(self):
        """
        Test saving a file
        """
        name = "test_storage_save.txt"
        content = ContentFile("new content")
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/plain",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_save_non_seekable(self):
        """
        Test saving a non-seekable file
        """
        name = "test_storage_save.txt"
        content = NonSeekableContentFile("new content")
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/plain",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_save_with_default_acl(self):
        """
        Test saving a file with user defined ACL.
        """
        name = "test_storage_save.txt"
        content = ContentFile("new content")
        self.storage.default_acl = "private"
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/plain",
                "ACL": "private",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_object_parameters_not_overwritten_by_default(self):
        """
        Test saving a file with user defined ACL.
        """
        name = "test_storage_save.txt"
        content = ContentFile("new content")
        self.storage.default_acl = "public-read"
        self.storage.object_parameters = {"ACL": "private"}
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/plain",
                "ACL": "private",
            },
            Config=self.storage.transfer_config,
        )

    def test_content_type(self):
        """
        Test saving a file with a None content type.
        """
        name = "test_image.jpg"
        content = ContentFile("data")
        content.content_type = None
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "image/jpeg",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_save_gzipped(self):
        """
        Test saving a gzipped file
        """
        name = "test_storage_save.gz"
        content = ContentFile("I am gzip'd")
        self.storage.save(name, content)
        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_once_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "application/octet-stream",
                "ContentEncoding": "gzip",
            },
            Config=self.storage.transfer_config,
        )

    def test_content_type_set_explicitly(self):
        name = "test_file.gz"
        content = ContentFile("data")

        def get_object_parameters(name):
            return {"ContentType": "application/gzip"}

        self.storage.get_object_parameters = get_object_parameters
        self.storage.save(name, content)

        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "application/gzip",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_save_gzipped_non_seekable(self):
        """
        Test saving a gzipped file
        """
        name = "test_storage_save.gz"
        content = NonSeekableContentFile("I am gzip'd")
        self.storage.save(name, content)
        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_once_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "application/octet-stream",
                "ContentEncoding": "gzip",
            },
            Config=self.storage.transfer_config,
        )

    def test_storage_save_gzip(self):
        """
        Test saving a file with gzip enabled.
        """
        self.storage.gzip = True
        name = "test_storage_save.css"
        content = ContentFile("I should be gzip'd")
        self.storage.save(name, content)
        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/css",
                "ContentEncoding": "gzip",
            },
            Config=self.storage.transfer_config,
        )
        args, kwargs = obj.upload_fileobj.call_args
        content = args[0]
        zfile = gzip.GzipFile(mode="rb", fileobj=content)
        self.assertEqual(zfile.read(), b"I should be gzip'd")

    def test_storage_save_gzip_twice(self):
        """
        Test saving the same file content twice with gzip enabled.
        """
        # Given
        self.storage.gzip = True
        name = "test_storage_save.css"
        content = ContentFile("I should be gzip'd")

        # When
        self.storage.save(name, content)
        self.storage.save("test_storage_save_2.css", content)

        # Then
        obj = self.storage.bucket.Object.return_value
        obj.upload_fileobj.assert_called_with(
            mock.ANY,
            ExtraArgs={
                "ContentType": "text/css",
                "ContentEncoding": "gzip",
            },
            Config=self.storage.transfer_config,
        )
        args, kwargs = obj.upload_fileobj.call_args
        content = args[0]
        zfile = gzip.GzipFile(mode="rb", fileobj=content)
        self.assertEqual(zfile.read(), b"I should be gzip'd")

    def test_compress_content_len(self):
        """
        Test that file returned by _compress_content() is readable.
        """
        self.storage.gzip = True
        content = ContentFile(b"I should be gzip'd")
        content = self.storage._compress_content(content)
        self.assertTrue(len(content.read()) > 0)

    def test_storage_open_read_string(self):
        """
        Test opening a file in "r" mode (ie reading as string, not bytes)
        """
        name = "test_open_read_string.txt"
        with self.storage.open(name, "r") as file:
            content_str = file.read()
            self.assertEqual(content_str, "")

    def test_storage_open_write(self):
        """
        Test opening a file in write mode
        """
        name = "test_open_for_writïng.txt"
        content = "new content"

        # Set the encryption flag used for multipart uploads
        self.storage.object_parameters = {
            "ServerSideEncryption": "AES256",
            "StorageClass": "REDUCED_REDUNDANCY",
            "ACL": "public-read",
        }

        with self.storage.open(name, "wb") as file:
            self.storage.bucket.Object.assert_called_with(name)
            obj = self.storage.bucket.Object.return_value
            # Set the name of the mock object
            obj.key = name

            multipart = obj.initiate_multipart_upload.return_value
            multipart.Part.return_value.upload.side_effect = [
                {"ETag": "123"},
            ]
            file.write(content)
            obj.initiate_multipart_upload.assert_called_with(
                ACL="public-read",
                ContentType="text/plain",
                ServerSideEncryption="AES256",
                StorageClass="REDUCED_REDUNDANCY",
            )

        multipart.Part.assert_called_with(1)
        part = multipart.Part.return_value
        part.upload.assert_called_with(Body=content.encode())
        multipart.complete.assert_called_once_with(
            MultipartUpload={"Parts": [{"ETag": "123", "PartNumber": 1}]}
        )

    def test_write_bytearray(self):
        """Test that bytearray write exactly (no extra "bytearray" from stringify)."""
        name = "saved_file.bin"
        content = bytearray(b"content")
        with self.storage.open(name, "wb") as file:
            obj = self.storage.bucket.Object.return_value
            # Set the name of the mock object
            obj.key = name
            bytes_written = file.write(content)
            self.assertEqual(len(content), bytes_written)

    def test_storage_open_no_write(self):
        """
        Test opening file in write mode and closing without writing.

        A file should be created as by obj.put(...).
        """
        name = "test_open_no_write.txt"

        # Set the encryption flag used for puts
        self.storage.object_parameters = {
            "ServerSideEncryption": "AES256",
            "StorageClass": "REDUCED_REDUNDANCY",
        }

        with self.storage.open(name, "wb"):
            self.storage.bucket.Object.assert_called_with(name)
            obj = self.storage.bucket.Object.return_value
            obj.load.side_effect = ClientError(
                {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 404}},
                "head_bucket",
            )

            # Set the name of the mock object
            obj.key = name

        obj.load.assert_called_once_with()
        obj.put.assert_called_once_with(
            Body=b"",
            ContentType="text/plain",
            ServerSideEncryption="AES256",
            StorageClass="REDUCED_REDUNDANCY",
        )

    def test_storage_open_no_overwrite_existing(self):
        """
        Test opening an existing file in write mode and closing without writing.
        """
        name = "test_open_no_overwrite_existing.txt"

        # Set the encryption flag used for puts
        self.storage.object_parameters = {
            "ServerSideEncryption": "AES256",
            "StorageClass": "REDUCED_REDUNDANCY",
        }

        with self.storage.open(name, "wb"):
            self.storage.bucket.Object.assert_called_with(name)
            obj = self.storage.bucket.Object.return_value

            # Set the name of the mock object
            obj.key = name

        obj.load.assert_called_once_with()
        obj.put.assert_not_called()

    def test_storage_write_beyond_buffer_size(self):
        """
        Test writing content that exceeds the buffer size
        """
        name = "test_open_for_writïng_beyond_buffer_size.txt"

        # Set the encryption flag used for multipart uploads
        self.storage.object_parameters = {
            "ServerSideEncryption": "AES256",
            "StorageClass": "REDUCED_REDUNDANCY",
        }

        with self.storage.open(name, "wb") as file:
            self.storage.bucket.Object.assert_called_with(name)
            obj = self.storage.bucket.Object.return_value
            # Set the name of the mock object
            obj.key = name

            # Initiate the multipart upload
            file.write("")
            obj.initiate_multipart_upload.assert_called_with(
                ContentType="text/plain",
                ServerSideEncryption="AES256",
                StorageClass="REDUCED_REDUNDANCY",
            )
            multipart = obj.initiate_multipart_upload.return_value

            # Write content at least twice as long as the buffer size
            written_content = ""
            counter = 1
            multipart.Part.return_value.upload.side_effect = [
                {"ETag": "123"},
                {"ETag": "456"},
            ]
            while len(written_content) < 2 * file.buffer_size:
                content = "hello, aws {counter}\n".format(counter=counter)
                # Write more than just a few bytes in each iteration to keep the
                # test reasonably fast
                content += "*" * int(file.buffer_size / 10)
                file.write(content)
                written_content += content
                counter += 1

        self.assertListEqual(
            multipart.Part.call_args_list, [mock.call(1), mock.call(2)]
        )
        part = multipart.Part.return_value
        uploaded_content = "".join(
            args_list[1]["Body"].decode() for args_list in part.upload.call_args_list
        )
        self.assertEqual(uploaded_content, written_content)
        multipart.complete.assert_called_once_with(
            MultipartUpload={
                "Parts": [
                    {"ETag": "123", "PartNumber": 1},
                    {"ETag": "456", "PartNumber": 2},
                ]
            }
        )

    def test_storage_exists(self):
        self.assertTrue(self.storage.exists("file.txt"))
        self.storage.connection.meta.client.head_object.assert_called_with(
            Bucket=self.storage.bucket_name,
            Key="file.txt",
        )

    def test_storage_exists_ssec(self):
        params = {"SSECustomerKey": "xyz", "CacheControl": "never"}
        self.storage.get_object_parameters = lambda name: params
        self.storage.file_overwrite = False
        self.assertTrue(self.storage.exists("file.txt"))
        self.storage.connection.meta.client.head_object.assert_called_with(
            Bucket=self.storage.bucket_name, Key="file.txt", SSECustomerKey="xyz"
        )

    def test_storage_exists_false(self):
        self.storage.connection.meta.client.head_object.side_effect = ClientError(
            {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 404}},
            "HeadObject",
        )
        self.assertFalse(self.storage.exists("file.txt"))
        self.storage.connection.meta.client.head_object.assert_called_with(
            Bucket=self.storage.bucket_name,
            Key="file.txt",
        )

    def test_storage_exists_other_error_reraise(self):
        self.storage.connection.meta.client.head_object.side_effect = ClientError(
            {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 403}},
            "HeadObject",
        )
        with self.assertRaises(ClientError) as cm:
            self.storage.exists("file.txt")

        self.assertEqual(
            cm.exception.response["ResponseMetadata"]["HTTPStatusCode"], 403
        )

    def test_storage_delete(self):
        self.storage.delete("path/to/file.txt")
        self.storage.bucket.Object.assert_called_with("path/to/file.txt")
        self.storage.bucket.Object.return_value.delete.assert_called_with()

    def test_storage_delete_does_not_exist(self):
        self.storage.bucket.Object("file.txt").delete.side_effect = ClientError(
            {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 404}},
            "DeleteObject",
        )
        self.storage.delete("file.txt")
        # No problem

    def test_storage_delete_other_error_reraise(self):
        self.storage.bucket.Object("file.txt").delete.side_effect = ClientError(
            {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 403}},
            "DeleteObject",
        )
        with self.assertRaises(ClientError) as cm:
            self.storage.delete("file.txt")

        self.assertEqual(
            cm.exception.response["ResponseMetadata"]["HTTPStatusCode"], 403
        )

    def test_storage_listdir_base(self):
        # Files:
        #   some/path/1.txt
        #   2.txt
        #   other/path/3.txt
        #   4.txt
        pages = [
            {
                "CommonPrefixes": [
                    {"Prefix": "some"},
                    {"Prefix": "other"},
                ],
                "Contents": [
                    {"Key": "2.txt"},
                    {"Key": "4.txt"},
                ],
            },
        ]

        paginator = mock.MagicMock()
        paginator.paginate.return_value = pages
        self.storage._connections.connection.meta.client.get_paginator.return_value = (
            paginator
        )

        dirs, files = self.storage.listdir("")
        paginator.paginate.assert_called_with(
            Bucket=settings.AWS_STORAGE_BUCKET_NAME, Delimiter="/", Prefix=""
        )

        self.assertEqual(dirs, ["some", "other"])
        self.assertEqual(files, ["2.txt", "4.txt"])

    def test_storage_listdir_subdir(self):
        # Files:
        #   some/path/1.txt
        #   some/2.txt
        pages = [
            {
                "CommonPrefixes": [
                    {"Prefix": "some/path"},
                ],
                "Contents": [
                    {"Key": "some/2.txt"},
                ],
            },
        ]

        paginator = mock.MagicMock()
        paginator.paginate.return_value = pages
        self.storage._connections.connection.meta.client.get_paginator.return_value = (
            paginator
        )

        dirs, files = self.storage.listdir("some/")
        paginator.paginate.assert_called_with(
            Bucket=settings.AWS_STORAGE_BUCKET_NAME, Delimiter="/", Prefix="some/"
        )

        self.assertEqual(dirs, ["path"])
        self.assertEqual(files, ["2.txt"])

    def test_storage_listdir_empty(self):
        # Files:
        #   dir/
        pages = [
            {
                "Contents": [
                    {"Key": "dir/"},
                ],
            },
        ]

        paginator = mock.MagicMock()
        paginator.paginate.return_value = pages
        self.storage._connections.connection.meta.client.get_paginator.return_value = (
            paginator
        )

        dirs, files = self.storage.listdir("dir/")
        paginator.paginate.assert_called_with(
            Bucket=settings.AWS_STORAGE_BUCKET_NAME, Delimiter="/", Prefix="dir/"
        )

        self.assertEqual(dirs, [])
        self.assertEqual(files, [])

    def test_storage_size(self):
        obj = self.storage.bucket.Object.return_value
        obj.content_length = 4098

        name = "file.txt"
        self.assertEqual(self.storage.size(name), obj.content_length)

    def test_storage_size_not_exists(self):
        self.storage.bucket.Object.side_effect = ClientError(
            {"Error": {}, "ResponseMetadata": {"HTTPStatusCode": 404}},
            "HeadObject",
        )
        name = "file.txt"
        with self.assertRaisesMessage(
            FileNotFoundError, "File does not exist: file.txt"
        ):
            self.storage.size(name)

    def test_storage_mtime(self):
        # Test both USE_TZ cases
        for use_tz in (True, False):
            with self.settings(USE_TZ=use_tz):
                self._test_storage_mtime(use_tz)

    def _test_storage_mtime(self, use_tz):
        obj = self.storage.bucket.Object.return_value
        obj.last_modified = datetime.datetime.now(datetime.timezone.utc)

        name = "file.txt"
        self.assertIs(
            settings.USE_TZ,
            is_aware(self.storage.get_modified_time(name)),
            (
                "{} datetime object expected from get_modified_time() when USE_TZ={}"
            ).format(("Naive", "Aware")[settings.USE_TZ], settings.USE_TZ),
        )

    def test_storage_url(self):
        name = "test_storage_size.txt"
        url = "http://aws.amazon.com/%s" % name
        self.storage.connection.meta.client.generate_presigned_url.return_value = url
        self.storage.bucket.name = "bucket"
        self.assertEqual(self.storage.url(name), url)
        self.storage.connection.meta.client.generate_presigned_url.assert_called_with(
            "get_object",
            Params={"Bucket": self.storage.bucket.name, "Key": name},
            ExpiresIn=self.storage.querystring_expire,
            HttpMethod=None,
        )

        custom_expire = 123
        self.assertEqual(self.storage.url(name, expire=custom_expire), url)
        self.storage.connection.meta.client.generate_presigned_url.assert_called_with(
            "get_object",
            Params={"Bucket": self.storage.bucket.name, "Key": name},
            ExpiresIn=custom_expire,
            HttpMethod=None,
        )

        custom_method = "HEAD"
        self.assertEqual(self.storage.url(name, http_method=custom_method), url)
        self.storage.connection.meta.client.generate_presigned_url.assert_called_with(
            "get_object",
            Params={"Bucket": self.storage.bucket.name, "Key": name},
            ExpiresIn=self.storage.querystring_expire,
            HttpMethod=custom_method,
        )

    def test_url_unsigned(self):
        self.storage.querystring_auth = False
        self.storage.url("test_name")
        self.storage.unsigned_connection.meta.client.generate_presigned_url.assert_called_once()

    def test_url_protocol(self):
        self.assertFalse(hasattr(settings, "AWS_S3_URL_PROTOCOL"))
        self.assertEqual(self.storage.url_protocol, "https:")

        with override_settings(AWS_S3_URL_PROTOCOL=None):
            storage = s3.S3Storage()
            self.assertEqual(storage.url_protocol, "https:")

    @mock.patch("storages.backends.s3.datetime")
    def test_storage_url_custom_domain_signed_urls(self, dt):
        key_id = "test-key"
        filename = "file.txt"
        pem = dedent(
            """\
            -----BEGIN RSA PRIVATE KEY-----
            MIICWwIBAAKBgQCXVuwcMk+JmVSKuQ1K4dZx4Z1dEcRQgTlqvhAyljIpttXlZh2/
            fD3GkJCiqfwEmo+cdNK/LFzRj/CX8Wz1z1lH2USONpG6sAkotkatCbejiItDu5y6
            janGJHfuWXu6B/o9gwZylU1gIsePY3lLNk+r9QhXUO4jXw6zLJftVwKPhQIDAQAB
            AoGAbpkRV9HUmoQ5al+uPSkp5HOy4s8XHpYxdbaMc8ubwSxiyJCF8OhE5RXE/Xso
            N90UUox1b0xmUKfWddPzgvgTD/Ub7D6Ukf+nVWDX60tWgNxICAUHptGL3tWweaAy
            H+0+vZ0TzvTt9r00vW0FzO7F8X9/Rs1ntDRLtF3RCCxdq0kCQQDHFu+t811lCvy/
            67rMEKGvNsNNSTrzOrNr3PqUrCnOrzKazjFVjsKv5VzI/U+rXGYKWJsMpuCFiHZ3
            DILUC09TAkEAwpm2S6MN6pzn9eY6pmhOxZ+GQGGRUkKZfC1GDxaRSRb8sKTjptYw
            WSemJSxiDzdj3Po2hF0lbhkpJgUq6xnCxwJAZgHHfn5CLSJrDD7Q7/vZi/foK3JJ
            BRTfl3Wa4pAvv5meuRjKyEakVBGV79lyd5+ZHNX3Y40hXunjoO3FHrZIxwJAdRzu
            waxahrRxQOKSr20c4wAzWnGddIUSO9I/VHs/al5EKsbBHrnOlQkwizSfuwqZtfZ7
            csNf8FeCFRiNELoLJwJAZxWBE2+8J9VW9AQ0SE7j4FyM/B8FvRhF5PLAAsw/OxHO
            SxiFP7Ptdac1tm5H5zOqaqSHWphI19HNNilXKmxuCA==
            -----END RSA PRIVATE KEY-----"""
        ).encode("ascii")

        url = "https://mock.cloudfront.net/file.txt"
        signed_url = (
            url
            + "?Expires=3600&Signature=DbqVgh3FHtttQxof214tSAVE8Nqn3Q4Ii7eR3iykbOqAPbV"
            "89HC3EB~0CWxarpLNtbfosS5LxiP5EutriM7E8uR4Gm~UVY-PFUjPcwqdnmAiKJF0EVs7koJc"
            "MR8MKDStuWfFKVUPJ8H7ORYTOrixyHBV2NOrpI6SN5UX6ctNM50_&Key-Pair-Id=test-key"
        )

        self.storage.custom_domain = "mock.cloudfront.net"

        for pem_to_signer in (s3._use_cryptography_signer(), s3._use_rsa_signer()):
            self.storage.cloudfront_signer = pem_to_signer(key_id, pem)
            self.storage.querystring_auth = False
            self.assertEqual(self.storage.url(filename), url)

            self.storage.querystring_auth = True
            dt.utcnow.return_value = datetime.datetime.utcfromtimestamp(0)
            self.assertEqual(self.storage.url(filename), signed_url)

    def test_generated_url_is_encoded(self):
        self.storage.custom_domain = "mock.cloudfront.net"
        filename = "whacky & filename.mp4"
        url = self.storage.url(filename)
        parsed_url = urlparse(url)
        self.assertEqual(parsed_url.path, "/whacky%20%26%20filename.mp4")
        self.assertFalse(self.storage.bucket.meta.client.generate_presigned_url.called)

    def test_special_characters(self):
        self.storage.custom_domain = "mock.cloudfront.net"

        name = "ãlöhâ.jpg"
        content = ContentFile("new content")
        self.storage.save(name, content)
        self.storage.bucket.Object.assert_called_once_with(name)

        url = self.storage.url(name)
        parsed_url = urlparse(url)
        self.assertEqual(parsed_url.path, "/%C3%A3l%C3%B6h%C3%A2.jpg")

    def test_custom_domain_parameters(self):
        self.storage.custom_domain = "mock.cloudfront.net"
        filename = "filename.mp4"
        url = self.storage.url(filename, parameters={"version": 10})
        parsed_url = urlparse(url)
        self.assertEqual(parsed_url.path, "/filename.mp4")
        self.assertEqual(parsed_url.query, "version=10")

    @skipIf(threading is None, "Test requires threading")
    def test_connection_threading(self):
        connections = []

        def thread_storage_connection():
            connections.append(self.storage.connection)

        for _ in range(2):
            t = threading.Thread(target=thread_storage_connection)
            t.start()
            t.join()

        # Connection for each thread needs to be unique
        self.assertIsNot(connections[0], connections[1])

    def test_location_leading_slash(self):
        msg = (
            "S3Storage.location cannot begin with a leading slash. "
            "Found '/'. Use '' instead."
        )
        with self.assertRaises(ImproperlyConfigured, msg=msg):
            s3.S3Storage(location="/")

    def test_override_settings(self):
        with override_settings(AWS_LOCATION="foo1"):
            storage = s3.S3Storage()
            self.assertEqual(storage.location, "foo1")
        with override_settings(AWS_LOCATION="foo2"):
            storage = s3.S3Storage()
            self.assertEqual(storage.location, "foo2")

    def test_override_class_variable(self):
        class MyStorage1(s3.S3Storage):
            location = "foo1"

        storage = MyStorage1()
        self.assertEqual(storage.location, "foo1")

        class MyStorage2(s3.S3Storage):
            location = "foo2"

        storage = MyStorage2()
        self.assertEqual(storage.location, "foo2")

    def test_override_init_argument(self):
        storage = s3.S3Storage(location="foo1")
        self.assertEqual(storage.location, "foo1")
        storage = s3.S3Storage(location="foo2")
        self.assertEqual(storage.location, "foo2")

    def test_use_threads_false(self):
        with override_settings(AWS_S3_USE_THREADS=False):
            storage = s3.S3Storage()
            self.assertFalse(storage.transfer_config.use_threads)

    def test_transfer_config(self):
        storage = s3.S3Storage()
        self.assertTrue(storage.transfer_config.use_threads)

        transfer_config = boto3.s3.transfer.TransferConfig(use_threads=False)
        with override_settings(AWS_S3_TRANSFER_CONFIG=transfer_config):
            storage = s3.S3Storage()
            self.assertFalse(storage.transfer_config.use_threads)

    def test_cloudfront_config(self):
        # Valid configs
        storage = s3.S3Storage()
        self.assertIsNone(storage.cloudfront_signer)

        key_id = "test-id"
        pem = dedent(
            """\
            -----BEGIN RSA PRIVATE KEY-----
            MIICWwIBAAKBgQCXVuwcMk+JmVSKuQ1K4dZx4Z1dEcRQgTlqvhAyljIpttXlZh2/
            fD3GkJCiqfwEmo+cdNK/LFzRj/CX8Wz1z1lH2USONpG6sAkotkatCbejiItDu5y6
            janGJHfuWXu6B/o9gwZylU1gIsePY3lLNk+r9QhXUO4jXw6zLJftVwKPhQIDAQAB
            AoGAbpkRV9HUmoQ5al+uPSkp5HOy4s8XHpYxdbaMc8ubwSxiyJCF8OhE5RXE/Xso
            N90UUox1b0xmUKfWddPzgvgTD/Ub7D6Ukf+nVWDX60tWgNxICAUHptGL3tWweaAy
            H+0+vZ0TzvTt9r00vW0FzO7F8X9/Rs1ntDRLtF3RCCxdq0kCQQDHFu+t811lCvy/
            67rMEKGvNsNNSTrzOrNr3PqUrCnOrzKazjFVjsKv5VzI/U+rXGYKWJsMpuCFiHZ3
            DILUC09TAkEAwpm2S6MN6pzn9eY6pmhOxZ+GQGGRUkKZfC1GDxaRSRb8sKTjptYw
            WSemJSxiDzdj3Po2hF0lbhkpJgUq6xnCxwJAZgHHfn5CLSJrDD7Q7/vZi/foK3JJ
            BRTfl3Wa4pAvv5meuRjKyEakVBGV79lyd5+ZHNX3Y40hXunjoO3FHrZIxwJAdRzu
            waxahrRxQOKSr20c4wAzWnGddIUSO9I/VHs/al5EKsbBHrnOlQkwizSfuwqZtfZ7
            csNf8FeCFRiNELoLJwJAZxWBE2+8J9VW9AQ0SE7j4FyM/B8FvRhF5PLAAsw/OxHO
            SxiFP7Ptdac1tm5H5zOqaqSHWphI19HNNilXKmxuCA==
            -----END RSA PRIVATE KEY-----"""
        ).encode("ascii")

        with override_settings(AWS_CLOUDFRONT_KEY_ID=key_id, AWS_CLOUDFRONT_KEY=pem):
            storage = s3.S3Storage()
            self.assertIsNotNone(storage.cloudfront_signer)

            # allow disabling cloudfront signing
            storage = s3.S3Storage(cloudfront_signer=None)
            self.assertIsNone(storage.cloudfront_signer)

            # allow disabling cloudfront signing in subclass
            class Storage(s3.S3Storage):
                cloudfront_signer = None

            self.assertIsNone(Storage().cloudfront_signer)

        storage = s3.S3Storage(cloudfront_key_id=key_id, cloudfront_key=pem)
        self.assertIsNotNone(storage.cloudfront_signer)

        cloudfront_signer = storage.get_cloudfront_signer(key_id, pem)
        storage = s3.S3Storage(cloudfront_signer=cloudfront_signer)
        self.assertIsNotNone(storage.cloudfront_signer)

        with override_settings(AWS_CLOUDFRONT_KEY_ID=key_id):
            storage = s3.S3Storage(cloudfront_key=pem)
            self.assertIsNotNone(storage.cloudfront_signer)

        # Invalid configs
        msg = (
            "Both AWS_CLOUDFRONT_KEY_ID/cloudfront_key_id and "
            "AWS_CLOUDFRONT_KEY/cloudfront_key must be provided together."
        )
        with override_settings(AWS_CLOUDFRONT_KEY_ID=key_id):
            with self.assertRaisesMessage(ImproperlyConfigured, msg):
                storage = s3.S3Storage()

        with override_settings(AWS_CLOUDFRONT_KEY=pem):
            with self.assertRaisesMessage(ImproperlyConfigured, msg):
                storage = s3.S3Storage()

        with self.assertRaisesMessage(ImproperlyConfigured, msg):
            storage = s3.S3Storage(cloudfront_key_id=key_id)

        with self.assertRaisesMessage(ImproperlyConfigured, msg):
            storage = s3.S3Storage(cloudfront_key=pem)

    def test_auth_config(self):
        # Valid configs
        with override_settings(
            AWS_S3_ACCESS_KEY_ID="foo", AWS_S3_SECRET_ACCESS_KEY="boo"
        ):
            storage = s3.S3Storage()
            self.assertEqual(storage.access_key, "foo")
            self.assertEqual(storage.secret_key, "boo")

        with override_settings(AWS_ACCESS_KEY_ID="foo", AWS_SECRET_ACCESS_KEY="boo"):
            storage = s3.S3Storage()
            self.assertEqual(storage.access_key, "foo")
            self.assertEqual(storage.secret_key, "boo")

        with mock.patch.dict(
            os.environ,
            {"AWS_S3_ACCESS_KEY_ID": "foo", "AWS_S3_SECRET_ACCESS_KEY": "boo"},
        ):
            storage = s3.S3Storage()
            self.assertEqual(storage.access_key, "foo")
            self.assertEqual(storage.secret_key, "boo")

        with mock.patch.dict(
            os.environ, {"AWS_ACCESS_KEY_ID": "foo", "AWS_SECRET_ACCESS_KEY": "boo"}
        ):
            storage = s3.S3Storage()
            self.assertEqual(storage.access_key, "foo")
            self.assertEqual(storage.secret_key, "boo")

        storage = s3.S3Storage(access_key="foo", secret_key="boo")
        self.assertEqual(storage.access_key, "foo")
        self.assertEqual(storage.secret_key, "boo")

        # Invalid configs
        msg = (
            "AWS_S3_SESSION_PROFILE/session_profile should not be provided with "
            "AWS_S3_ACCESS_KEY_ID/access_key and AWS_S3_SECRET_ACCESS_KEY/secret_key"
        )
        with override_settings(
            AWS_ACCESS_KEY_ID="foo",
            AWS_SECRET_ACCESS_KEY="boo",
            AWS_S3_SESSION_PROFILE="moo",
        ):
            with self.assertRaisesMessage(ImproperlyConfigured, msg):
                storage = s3.S3Storage()

        with self.assertRaisesMessage(ImproperlyConfigured, msg):
            storage = s3.S3Storage(
                access_key="foo", secret_key="boo", session_profile="moo"
            )

    def test_security_token(self):
        with override_settings(AWS_SESSION_TOKEN="baz"):
            storage = s3.S3Storage()
            self.assertEqual(storage.security_token, "baz")

        with override_settings(AWS_SECURITY_TOKEN="baz"):
            storage = s3.S3Storage()
            self.assertEqual(storage.security_token, "baz")

        with mock.patch.dict(
            os.environ,
            {"AWS_SESSION_TOKEN": "baz"},
        ):
            storage = s3.S3Storage()
            self.assertEqual(storage.security_token, "baz")

        with mock.patch.dict(
            os.environ,
            {"AWS_SECURITY_TOKEN": "baz"},
        ):
            storage = s3.S3Storage()
            self.assertEqual(storage.security_token, "baz")


class S3StaticStorageTests(TestCase):
    def setUp(self):
        self.storage = s3.S3StaticStorage()
        self.storage._connections.connection = mock.MagicMock()

    def test_querystring_auth(self):
        self.assertFalse(self.storage.querystring_auth)


class S3ManifestStaticStorageTests(TestCase):
    def setUp(self):
        self.storage = S3ManifestStaticStorageTestStorage()
        self.storage._connections.connection = mock.MagicMock()

    def test_querystring_auth(self):
        self.assertFalse(self.storage.querystring_auth)

    def test_save(self):
        self.storage.save("x.txt", ContentFile(b"abc"))


class S3FileTests(TestCase):
    # Remove the override_settings after Python3.7 is dropped
    @override_settings(AWS_S3_OBJECT_PARAMETERS={"ContentType": "text/html"})
    def setUp(self) -> None:
        self.storage = s3.S3Storage()
        self.storage._connections.connection = mock.MagicMock()

    def test_loading_ssec(self):
        params = {"SSECustomerKey": "xyz", "CacheControl": "never"}
        self.storage.get_object_parameters = lambda name: params

        filtered = {"SSECustomerKey": "xyz"}
        f = s3.S3File("test", "r", self.storage)
        f.obj.load.assert_called_once_with(**filtered)

        f.file
        f.obj.download_fileobj.assert_called_once_with(
            mock.ANY, ExtraArgs=filtered, Config=self.storage.transfer_config
        )

    def test_closed(self):
        with s3.S3File("test", "wb", self.storage) as f:
            with self.subTest("after init"):
                self.assertFalse(f.closed)

            with self.subTest("after file access"):
                # Ensure _get_file has been called
                f.file
                self.assertFalse(f.closed)

            with self.subTest("after close"):
                f.close()
                self.assertTrue(f.closed)

            with self.subTest("reopening"):
                f.file
                self.assertFalse(f.closed)

    def test_reopening(self):
        f = s3.S3File("test", "wb", self.storage)

        with f.open() as fp:
            fp.write(b"xyz")

        with f.open() as fp:
            fp.write(b"xyz")

        # Properties are reset
        self.assertEqual(f._write_counter, 0)
        self.assertEqual(f._raw_bytes_written, 0)
        self.assertFalse(f._is_dirty)
        self.assertIsNone(f._multipart)


@mock_s3
class S3StorageTestsWithMoto(TestCase):
    """
    Using mock_s3 as a class decorator automatically decorates methods,
    but NOT classmethods or staticmethods.
    """

    def setUp(cls):
        super().setUp()

        cls.storage = s3.S3Storage()
        cls.bucket = cls.storage.connection.Bucket(settings.AWS_STORAGE_BUCKET_NAME)
        cls.bucket.create()

    def test_save_bytes_file(self):
        self.storage.save("bytes_file.txt", File(io.BytesIO(b"foo1")))

        self.assertEqual(
            b"foo1",
            self.bucket.Object("bytes_file.txt").get()["Body"].read(),
        )

    def test_save_string_file(self):
        self.storage.save("string_file.txt", File(io.StringIO("foo2")))

        self.assertEqual(
            b"foo2",
            self.bucket.Object("string_file.txt").get()["Body"].read(),
        )

    def test_save_bytes_content_file(self):
        self.storage.save("bytes_content.txt", ContentFile(b"foo3"))

        self.assertEqual(
            b"foo3",
            self.bucket.Object("bytes_content.txt").get()["Body"].read(),
        )

    def test_save_string_content_file(self):
        self.storage.save("string_content.txt", ContentFile("foo4"))

        self.assertEqual(
            b"foo4",
            self.bucket.Object("string_content.txt").get()["Body"].read(),
        )

    def test_content_type_guess(self):
        """
        Test saving a file where the ContentType is guessed from the filename.
        """
        name = "test_image.jpg"
        content = ContentFile(b"data")
        content.content_type = None
        self.storage.save(name, content)

        s3_object_fetched = self.bucket.Object(name).get()
        self.assertEqual(b"data", s3_object_fetched["Body"].read())
        self.assertEqual(s3_object_fetched["ContentType"], "image/jpeg")

    def test_content_type_attribute(self):
        """
        Test saving a file with a custom content type attribute.
        """
        content = ContentFile(b"data")
        content.content_type = "test/foo"
        self.storage.save("test_file", content)

        s3_object_fetched = self.bucket.Object("test_file").get()
        self.assertEqual(b"data", s3_object_fetched["Body"].read())
        self.assertEqual(s3_object_fetched["ContentType"], "test/foo")

    def test_content_type_not_detectable(self):
        """
        Test saving a file with no detectable content type.
        """
        content = ContentFile(b"data")
        content.content_type = None
        self.storage.save("test_file", content)

        s3_object_fetched = self.bucket.Object("test_file").get()
        self.assertEqual(b"data", s3_object_fetched["Body"].read())
        self.assertEqual(
            s3_object_fetched["ContentType"],
            s3.S3Storage.default_content_type,
        )

    def test_storage_open_reading_with_newlines(self):
        """Test file reading with "r" and "rb" and various newline characters."""
        name = "test_storage_open_read_with_newlines.txt"
        with io.BytesIO() as temp_file:
            temp_file.write(b"line1\nline2\r\nmore\rtext\n")
            self.storage.save(name, temp_file)
            file = self.storage.open(name, "r")
            content_str = file.read()
            file.close()
        self.assertEqual(content_str, "line1\nline2\nmore\ntext\n")

        with io.BytesIO() as temp_file:
            temp_file.write(b"line1\nline2\r\nmore\rtext\n")
            self.storage.save(name, temp_file)
            file = self.storage.open(name, "rb")
            content_str = file.read()
            file.close()
        self.assertEqual(content_str, b"line1\nline2\r\nmore\rtext\n")

        with io.BytesIO() as temp_file:
            temp_file.write(b"line1\nline2\r\nmore\rtext")
            self.storage.save(name, temp_file)
            file = self.storage.open(name, "r")
            content_lines = file.readlines()
            file.close()
        self.assertEqual(content_lines, ["line1\n", "line2\n", "more\n", "text"])

        with io.BytesIO() as temp_file:
            temp_file.write(b"line1\nline2\r\nmore\rtext")
            self.storage.save(name, temp_file)
            file = self.storage.open(name, "rb")
            content_lines = file.readlines()
            file.close()
        self.assertEqual(content_lines, [b"line1\n", b"line2\r\n", b"more\r", b"text"])


class TestBackwardsNames(TestCase):
    def test_importing(self):
        from storages.backends.s3boto3 import S3Boto3Storage  # noqa
        from storages.backends.s3boto3 import S3Boto3StorageFile  # noqa
        from storages.backends.s3boto3 import S3ManifestStaticStorage  # noqa
        from storages.backends.s3boto3 import S3StaticStorage  # noqa