From 684cafa2b8b4bcb6163bfb7344b509fac174cc76 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 28 Aug 2021 06:10:16 +0100 Subject: [PATCH] S3 - Allow uploads using a PUT request without content-type (#3699) --- moto/s3/responses.py | 15 +++++++++++---- tests/test_s3/test_s3.py | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 2f6234a59..db620b0a6 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -1187,10 +1187,17 @@ class ResponseObject(_TemplateEnvironmentMixin, ActionAuthenticatorMixin): else: # Flask server body = request.data - # when the data is being passed as a file - if request.files and not body: - for _, value in request.files.items(): - body = value.stream.read() + if not body: + # when the data is being passed as a file + if request.files: + for _, value in request.files.items(): + body = value.stream.read() + elif hasattr(request, "form"): + # Body comes through as part of the form, if no content-type is set on the PUT-request + # form = ImmutableMultiDict([('some data 123 321', '')]) + form = request.form + for k, _ in form.items(): + body = k if body is None: body = b"" diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py index 58874f52b..e6721689c 100644 --- a/tests/test_s3/test_s3.py +++ b/tests/test_s3/test_s3.py @@ -5186,6 +5186,22 @@ def test_object_headers(): res.should.have.key("BucketKeyEnabled") +if settings.TEST_SERVER_MODE: + + @mock_s3 + def test_upload_data_without_content_type(): + bucket = "mybucket" + s3 = boto3.client("s3") + s3.create_bucket(Bucket=bucket) + data_input = b"some data 123 321" + req = requests.put("http://localhost:5000/mybucket/test.txt", data=data_input) + req.status_code.should.equal(200) + + res = s3.get_object(Bucket=bucket, Key="test.txt") + data = res["Body"].read() + assert data == data_input + + @mock_s3 def test_get_object_versions_with_prefix(): bucket_name = "testbucket-3113"