skip to Main Content

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


  1. 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 the x-amz-meta header or set the content type explicitly during the upload.

    Login or Signup to reply.
  2. Try this:

    1. Policy Document: Ensure you have a policy document that includes conditions for the Content-Type.
    2. Include Content-Type in the form fields: The Content-Type must be included as a form field, and you must also ensure that your policy document allows for the Content-Type to be included.

    HTML Form:

    <form action="https://YOUR_S3_BUCKET.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
        <input type="hidden" name="key" value="path/to/your/file" />
        <input type="hidden" name="Content-Type" value="video/x-matroska;codecs=avc1,pcm" />
        <input type="hidden" name="AWSAccessKeyId" value="YOUR_AWS_ACCESS_KEY_ID" />
        <input type="hidden" name="acl" value="public-read" />
        <input type="hidden" name="success_action_redirect" value="http://localhost/success.html" />
        <input type="hidden" name="policy" value="YOUR_BASE64_ENCODED_POLICY_DOCUMENT" />
        <input type="hidden" name="signature" value="YOUR_SIGNATURE" />
        <input type="file" name="file" />
        <input type="submit" value="Upload to S3" />
    </form>
    

    Policy Document:

    {
      "expiration": "2023-12-30T12:00:00.000Z",
      "conditions": [
        {"bucket": "YOUR_S3_BUCKET"},
        ["starts-with", "$key", "path/to/your/file"],
        {"acl": "public-read"},
        ["starts-with", "$Content-Type", "video/x-matroska"],
        {"success_action_redirect": "http://localhost/success.html"},
        ["content-length-range", 0, 10485760]  // Max file size
      ]
    }
    

    You’ll need to base64 encode the policy document and sign it with your AWS Secret Access Key

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search