Repository: getmoto/moto

Issue:
Bucket.download_file downloads file with metadata
I am uploading a file and then downloading the same file.

The upload works fine, I am checking the content and is there where it fails

The file:
```
ID|COUNTRY
1|Foo
...
```

The upload code

```
s3_client.upload_file(file_name, bucket, object_name, ExtraArgs={"Metadata": {"foo": "boo"}, "ChecksumAlgorithm": "SHA256"})
```

The test failing

```
body = conn.Object(S3_BUCKET, "my_key.csv").get()['Body'].read().decode("utf-8")
assert body.startswith('ID|COUNTRY'), body
>>>
assert False
E        +  where False = <built-in method startswith of str object at 0x55a06e2966e0>('ID|COUNTRY')
E        +    where <built-in method startswith of str object at 0x55a06e2966e0> = '2f3\r\nID|COUNTRY...'
```

I would expect to have exactly the same body as I am saving via `pandas.to_csv`

The `2f3` is a random value as far I could see...

The downloaded files look like
```
E       AssertionError: 2f3 # this is new content
E         ID|COUNTRY
E         1|US
E         ...
E         # this is new content
E         0 # this is new content
E         x-amz-checksum-sha256:0QMsyrLjuMEaWcVrl26PZwuf0axTOR2XdUcU1uD0ayQ= # this is new content - from the meta? and where is my extra meta?
```


I am working around with...

```
# only in test suite
with open(_tmp_file, 'rb') as tf:
    content = tf.readlines()[1:(len(tf.readlines())-3)]
with open(_tmp_file, 'wb') as tfw:
    tfw.write(b'')
    tfw.writelines(content)
```


Discussion:
Thanks for raising this @andrescevp - looks like the ChecksumAlgorithm-parameter confuses Moto somehow. Marking it as a bug.
@bblommers awesome, thanks, I am bit short of time to create a proper PR sorry.
@bblommers this test fails later in CI/DI. `AWS_DEFAULT_REGION` is set in the envs and a custom region is set in the client.

```            error_code = parsed_response.get("Error", {}).get("Code")
            error_class = self.exceptions.from_code(error_code)
>           raise error_class(parsed_response, operation_name)
E           botocore.exceptions.ClientError: An error occurred (IllegalLocationConstraintException) when calling the CreateBucket operation: The unspecified location constraint is incompatible for the region specific endpoint this request was sent to.```
The `IllegalLocationConstraintException` occurs when using a non-default (i.e., not `us-east-`) region in the S3-client.
If you do want to use a non-default region, it is necessary to pass in the following config: 
```
bucket.create(
    CreateBucketConfiguration={"LocationConstraint": "us-west-1"}
)
```
Note that the same validation occurs when using AWS - so we're throwing the same error.

Resolving patch:
diff --git a/moto/s3/responses.py b/moto/s3/responses.py
--- a/moto/s3/responses.py
+++ b/moto/s3/responses.py
@@ -1104,6 +1104,15 @@ def _handle_v4_chunk_signatures(self, body, content_length):
             line = body_io.readline()
         return bytes(new_body)
 
+    def _handle_encoded_body(self, body, content_length):
+        body_io = io.BytesIO(body)
+        # first line should equal '{content_length}\r\n
+        body_io.readline()
+        # Body contains actual data next
+        return body_io.read(content_length)
+        # last line should equal
+        # amz-checksum-sha256:<..>\r\n
+
     @amzn_request_id
     def key_response(self, request, full_url, headers):
         # Key and Control are lumped in because splitting out the regex is too much of a pain :/
@@ -1384,12 +1393,22 @@ def _key_response_put(self, request, body, bucket_name, query, key_name):
         checksum_header = f"x-amz-checksum-{checksum_algorithm.lower()}"
         checksum_value = request.headers.get(checksum_header)
         if not checksum_value and checksum_algorithm:
-            search = re.search(r"x-amz-checksum-\w+:(\w+={1,2})", body.decode())
+            # Extract the checksum-value from the body first
+            search = re.search(b"x-amz-checksum-\w+:(\w+={1,2})", body)
             checksum_value = search.group(1) if search else None
 
         if checksum_value:
             response_headers.update({checksum_header: checksum_value})
 
+        # Extract the actual data from the body second
+        if (
+            request.headers.get("x-amz-content-sha256", None)
+            == "STREAMING-UNSIGNED-PAYLOAD-TRAILER"
+        ):
+            body = self._handle_encoded_body(
+                body, int(request.headers["x-amz-decoded-content-length"])
+            )
+
         bucket_key_enabled = request.headers.get(
             "x-amz-server-side-encryption-bucket-key-enabled", None
         )


Test patch:
diff --git a/tests/test_s3/red.jpg b/tests/test_s3/red.jpg
deleted file mode 100644
Binary files a/tests/test_s3/red.jpg and /dev/null differ
diff --git a/tests/test_s3/test_s3.py b/tests/test_s3/test_s3.py
--- a/tests/test_s3/test_s3.py
+++ b/tests/test_s3/test_s3.py
@@ -808,6 +808,24 @@ def test_upload_from_file_to_presigned_url():
     os.remove("text.txt")
 
 
+@mock_s3
+def test_upload_file_with_checksum_algorithm():
+    random_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\n\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\n"
+    with open("rb.tmp", mode="wb") as f:
+        f.write(random_bytes)
+    s3 = boto3.resource("s3", region_name=DEFAULT_REGION_NAME)
+    s3_client = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
+    bucket = "mybucket"
+    s3_client.create_bucket(Bucket=bucket)
+    s3_client.upload_file(
+        "rb.tmp", bucket, "my_key.csv", ExtraArgs={"ChecksumAlgorithm": "SHA256"}
+    )
+    os.remove("rb.tmp")
+
+    actual_content = s3.Object(bucket, "my_key.csv").get()["Body"].read()
+    assert random_bytes == actual_content
+
+
 @mock_s3
 def test_put_chunked_with_v4_signature_in_body():
     bucket_name = "mybucket"