Auto-detect MIME types on upload
Let Bitiful S4 detect object MIME types automatically
In 2024, we improved detection of M2TS and M4S video segment types to help you defend against increasingly common resource abuse attacks.
In the best practice "Upload files securely from the client", the JavaScript snippet at the end deliberately forces Content-Type to empty, because:
- When axios sends a PUT request, it adds a
Content-Typeofapplication/x-www-form-urlencodedby default, which differs from what S3 expects forContent-Typeon a PUT. - In the S3 protocol, if you include the file's MIME type as
Content-Typewhen uploading, it's saved as metadata in the S3 backend (telling S3 what MIME type the uploaded object is).
So the ideal approach might seem to be setting Content-Type to the MIME type of the file being uploaded, for example:
const config = {
headers: {
'Content-Type': 'image/jpeg',
},
};With AWS or OSS, the best you can do is probably to write reliable file-type detection in JavaScript, and tell the server which MIME type you intend to upload when you request the presigned URL:
const url = `/s3_upload_url`;
const params = {key: '1.jpg', mime: 'image/jpeg'};With Bitiful S4, you have a better option:
When Bitiful S4 receives an upload request with an empty Content-Type, it automatically determines the file's MIME type from both its extension and its bytes.
In AWS S3 and OSS, every such object becomes "application/octet-stream".
Unlike file.type in JavaScript, which relies only on the file extension, handing MIME detection over to S4 is more reliable and accurate.
All you need to do is:
- Delete your MIME detection code.
- Set Content-Type to null when you send the PUT request.