Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion moto/core/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ def setup_class(
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"] = [
Expand Down
11 changes: 11 additions & 0 deletions tests/test_s3/test_s3_file_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import SkipTest, TestCase

import boto3
import requests

from moto import mock_aws, settings
from moto.dynamodb.models import DynamoDBBackend
Expand Down Expand Up @@ -311,6 +312,16 @@ def test_update_versioned_object__while_looping(self):
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
Expand Down