I have an endpoint that uploads an image file to the server, then to S3.
When I run on localhost, the MultipartFile byte size is correct, and the upload is successful.
However, the moment I deploy it to my EC2 instance the uploaded file size is incorrect.
Controller Code
@PostMapping("/{id}/photos")
fun addPhotos(@PathVariable("id") id: Long,
@RequestParam("file") file: MultipartFile,
jwt: AuthenticationJsonWebToken) = ApiResponse.success(propertyBLL.addPhotos(id, file, jwt))
Within the PropertyBLL.addPhotos
method, printing file.size results in the wrong size.
Actual file size is 649305 bytes, however when uploaded to my prod server it reads as 1189763 bytes.
- My production server is an AWS EC2 instance, behind Https.
- The Spring application yml files are the same. The only configurations I overrode were the file max size properties.
- I'm using PostMan to Post the request. I'm passing the body as form-data, key named "file".
- Again, it works perfectly when running locally.
I did another test where I wrote the uploaded file to the server so I could compare.
Uploaded file's first n bytes in Hex editor:
EFBFBD50 4E470D0A 1A0A0000 000D4948 44520000 03000000 02400802 000000EF BFBDCC96 01000000 0467414D 410000EF BFBDEFBF BD0BEFBF BD610500 00002063 48524D00 007A2600
Original file's first n bytes:
89504E47 0D0A1A0A 0000000D 49484452 00000300 00000240 08020000 00B5CC96 01000000 0467414D 410000B1 8F0BFC61 05000000 20634852 4D00007A 26000080 840000FA 00000080
They both appear to have the text "PNG" in them and also have the ending EXtdate:modify/create markers.
Per Request, the core contents of addPhoto:
val metadata = ObjectMetadata()
metadata.contentLength = file.size
metadata.contentType = "image/png"
LOGGER.info("Uploading image of size {} bytes, name: {}", file.size, file.originalFilename)
val request = PutObjectRequest(awsProperties.cdnS3Bucket, imageName, file.inputStream, metadata)
awsSdk.putObject(request)
This works when I run web server locally. imageName is just a custom built name. There is other code involving hibernate models, but is not relevant.
Update
This appears to be Https/api proxy related. When I hit the EC2 node's http url, it works fine. However, when I go through the api proxy (https://api.thedomain.com), which proxies to the EC2 node, it fails. I will continue down this path.