boto3 + moto + s3 + Docker: "ValueError, "I/O operation on closed file""
See original GitHub issueThis simple test passes locally (when ran using py.test
)
@mock_s3
def test_read_data():
temp = tempfile.NamedTemporaryFile()
temp.write('Something')
temp.seek(0)
s3 = boto3.resource('s3')
s3.create_bucket(Bucket='mybucket')
s3.Object('mybucket', 'hello.txt').put(Body=open(temp.name, 'rb'))
obj = s3.Object(bucket_name='mybucket', key='hello.txt')
response = obj.get()
data = response['Body'].read()
assert data == 'Something'
However, the same test fails when run inside a Docker container (docker build .
):
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/botocore/endpoint.py", line 168, in _get_response
proxies=self.proxies, timeout=self.timeout)
File "/usr/local/lib/python2.7/site-packages/botocore/vendored/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/botocore/vendored/requests/adapters.py", line 370, in send
timeout=timeout
File "/usr/local/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 544, in urlopen
body=body, headers=headers)
File "/usr/local/lib/python2.7/site-packages/botocore/vendored/requests/packages/urllib3/connectionpool.py", line 372, in _make_request
httplib_response = conn.getresponse(buffering=True)
File "/usr/local/lib/python2.7/httplib.py", line 1129, in getresponse
response = self.response_class(*args, **kwds)
File "/usr/local/lib/python2.7/site-packages/botocore/awsrequest.py", line 45, in __init__
HTTPResponse.__init__(self, *args, **kwargs)
File "/usr/local/lib/python2.7/httplib.py", line 383, in __init__
self.fp = sock.makefile('rb')
File "/usr/local/lib/python2.7/site-packages/httpretty/core.py", line 332, in makefile
self._entry.fill_filekind(self.fd)
File "/usr/local/lib/python2.7/site-packages/httpretty/core.py", line 621, in fill_filekind
fk.write(utf8(item) + b'\n')
File "/usr/local/lib/python2.7/StringIO.py", line 213, in write
_complain_ifclosed(self.closed)
File "/usr/local/lib/python2.7/StringIO.py", line 40, in _complain_ifclosed
raise ValueError, "I/O operation on closed file"
ValueError: I/O operation on closed file
Maybe this issue should be posted to httppretty
instead of here.
Source code: boto3_pytest_docker.zip
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (4 by maintainers)
Top Results From Across the Web
boto3 + moto + s3 + Docker: "ValueError, "I/O ... - Bountysource
This simple test passes locally (when ran using py.test ) @mock_s3 def test_read_data(): temp = tempfile.
Read more >ValueError: I/O operation on closed file . Python, Django , Boto3
ValueError : I/O operation on closed file. Means the file you are working with has closed. If it's a Python PIL image, ...
Read more >[Solved] ValueError: I/O operation on closed file. - Python Pool
FAQs on ValueError: I/O operation on closed file No, you can't read and write on a closed file. If you try to do...
Read more >boto/boto3 - Gitter
Hi, Trying to upload a zip file to s3 but getting error. “ AttributeError: '_SharedFile' object has no attribute 'writing'” import boto3 import...
Read more >Release Notes — Airflow Documentation
Change default python executable to python3 for docker decorator (#21973) ... Use try/except when closing temporary file in task_runner (#18269).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I had a similar issue with StringIO and got rid of it by putting the contents of the file instead of the file itself.
Something like this:
Same issue with moto 1.1.21 (using boto3==1.4.7) Going back to 0.4.x fixes the issue
my code
failling test:
trace: