adobe / S3Mock

A simple mock implementation of the AWS S3 API startable as Docker image, TestContainer, JUnit 4 rule, JUnit Jupiter extension or TestNG listener
Apache License 2.0
832 stars 179 forks source link

CORS issue when uploading file using presigned POST URL #2106

Open fabiob opened 5 days ago

fabiob commented 5 days ago

Hi! I'm migrating some of our tests from Localstack to S3Mock (great work, btw!), but I've encountered an issue where one of our tests fails when uploading a file to S3Mock using a presigned POST URL.

The request includes the Access-Control-Allow-Methods: POST header, and this seems to cause the failure. I've tried reproducing the issue, and you can see the failing test case here: https://github.com/fabiob/S3Mock/commit/c7679fc8d8c84e3c7378f49f2d69741a6c71f7eb

What I've tried:

None of these attempts seem to resolve the issue. The test still fails when performing the OPTIONS request.

Any insights or workarounds from others who may have experienced this issue would be greatly appreciated!

References I've used:

afranken commented 3 days ago

@fabiob which URL are you trying to POST to?

The controllers are annotated with @CrossOrigin(origins = "*", exposedHeaders = "*") which means that the HTTP methods allowed default to the HTTP method declared by the controller function. There are only a few S3 APIs that allow POST requests, most APIs use PUT to modify or create data.

The only APIs supported by S3Mock using POST are deleteObjects, createMultipartUpload and completeMultipartUpload.

The reason why your test fails is because the URL used points to a bucket (https://github.com/fabiob/S3Mock/commit/c7679fc8d8c84e3c7378f49f2d69741a6c71f7eb#diff-830d0afe6486c2b436b422fabcb308511dab2b2eef4c1edf698108716a4d7b0dR228), and there are no APIs that allow a POST on a bucket.

afranken commented 3 days ago

if you change your test to use "PUT" instead, it succeeds, as there are two APIs (and controller functions) that allow a "PUT" on a bucket, for example putObjectLockConfiguration

afranken commented 3 days ago

You write "upload file" - do you mean "putObject"? This API needs a "PUT", not a "POST" request: https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html