diff --git a/storages/backends/s3boto3.py b/storages/backends/s3boto3.py index f1d4e1c3..5122aa93 100644 --- a/storages/backends/s3boto3.py +++ b/storages/backends/s3boto3.py @@ -167,6 +167,9 @@ def readline(self, *args, **kwargs): raise AttributeError("File was not opened in read mode.") return self._force_mode(super().readline(*args, **kwargs)) + def readlines(self): + return list(self) + def write(self, content): if 'w' not in self._mode: raise AttributeError("File was not opened in write mode.") diff --git a/tests/test_s3boto3.py b/tests/test_s3boto3.py index 842a346f..bf3dfe1e 100644 --- a/tests/test_s3boto3.py +++ b/tests/test_s3boto3.py @@ -1,5 +1,6 @@ import datetime import gzip +import io import pickle import threading from textwrap import dedent @@ -300,6 +301,25 @@ def test_storage_open_read_string(self): self.assertEqual(content_str, "") file.close() + def test_storage_open_readlines(self): + """ + Test readlines with file opened in "r" and "rb" modes + """ + name = 'test_open_readlines_string.txt' + with io.BytesIO() as temp_file: + temp_file.write(b"line1\nline2") + file = self.storage.open(name, "r") + file._file = temp_file + + content_lines = file.readlines() + self.assertEqual(content_lines, ["line1\n", "line2"]) + + temp_file.seek(0) + file = self.storage.open(name, "rb") + file._file = temp_file + content_lines = file.readlines() + self.assertEqual(content_lines, [b"line1\n", b"line2"]) + def test_storage_open_write(self): """ Test opening a file in write mode