S3: Form uploads: Ensure streams are closed properly (#7405)

This commit is contained in:
Bert Blommers 2024-02-28 21:03:47 +00:00 committed by GitHub
parent d2204f3cc2
commit ca28bdf702
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View File

@ -333,7 +333,8 @@ class BaseResponse(_TemplateEnvironmentMixin, ActionAuthenticatorMixin):
self.body = k
if hasattr(request, "files") and request.files:
for _, value in request.files.items():
self.body = value.stream
self.body = value.stream.read()
value.stream.close()
if querystring.get("key"):
filename = os.path.basename(request.files["file"].filename)
querystring["key"] = [

View File

@ -5,6 +5,7 @@ from functools import wraps
from unittest import SkipTest, TestCase
import boto3
import requests
from moto import mock_aws, settings
from moto.dynamodb.models import DynamoDBBackend
@ -311,6 +312,16 @@ class TestS3FileHandleClosuresUsingMocks(TestCase):
self.s3_client.put_object(Bucket="foo", Key="bar", Body="stuff")
self.s3_client.put_object(Bucket="foo", Key="bar", Body="stuff2")
@verify_zero_warnings
def test_upload_using_form(self):
with mock_aws():
self.s3_client.create_bucket(Bucket="foo")
requests.post(
"https://foo.s3.amazonaws.com/",
data={"key": "test-key"},
files={"file": ("tmp.txt", b"test content")},
)
def test_verify_key_can_be_copied_after_disposing():
# https://github.com/getmoto/moto/issues/5588