When using a form POST to upload a file to an S3 bucket, S3 ignores the Content-Type
/MIME type of the file.
The file is uploaded successfully. The file
form data field contains a Blob with a Content-Type
of video/x-matroska;codecs=avc1,pcm
. The key
form data field sets the key of where that file goes in the bucket. But, when I HEAD
or GET
that object later, its Content-Type
is just binary/octet-stream
.
Just for kicks, I added a form field named Content-Type
and duplicated the type of the Blob, but that didn’t work. I also tried ContentType
and content_type
, and those didn’t work either.
Is it just not possible to set the type via a form upload?
Here’s some of the raw request data so you can see that the data is indeed being sent to S3.
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ
Content-Disposition: form-data; name="key"
woot/woot/wootwootwoot
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ
Content-Disposition: form-data; name="file"; filename="woot/woot/wootwootwoot"
Content-Type: video/x-matroska;codecs=avc1,pcm
Eߣ£BB÷BòBóBmatroskaBBSgÿÿÿÿÿÿÿI©f*×±B@MChromeWAChromeT®kà®±×sÅoù¾¤=ÆA_PCM/FLOAT/IEEEáµG,Dbd ®«×sź<ø
V_MPEG4/ISO/AVCà°º8
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ
Content-Disposition: form-data; name="Content-Type"
video/x-matroska;codecs=avc1,pcm
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ
Content-Disposition: form-data; name="ContentType"
video/x-matroska;codecs=avc1,pcm
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ
Content-Disposition: form-data; name="content_type"
video/x-matroska;codecs=avc1,pcm
------WebKitFormBoundaryJJ9VA3o1bbQ3DIPJ--
2
Answers
If you are uploading files directly to an S3 bucket using a form POST, setting the Content-Type via the form data is not natively supported by S3. Instead, you can use
Content-Type
header in the HTTP request to set the MIME type of the object being uploaded. If you can use S3 pre-signed URLs, then you can use thex-amz-meta
header or set the content type explicitly during the upload.Try this:
HTML Form:
Policy Document:
You’ll need to base64 encode the policy document and sign it with your AWS Secret Access Key