Setting the Content-Type in direct to S3 upload using Rails and jQuery File Upload

后端 未结 3 1469
旧时难觅i
旧时难觅i 2020-12-16 16:16

I\'ve seen many questions here on this topic but nothing I\'ve come across has worked for me, so here I am posting another...

I\'m on Ruby on Rails trying to configu

相关标签:
3条回答
  • 2020-12-16 16:56

    Replace your add block with:

          fd["Content-Type"] = data.files[0].type;  
          data.formData = fd;
          data.submit();
    

    The callback is correct, but data.formData already took the original version of fd. Just set it again with your modified fd and you should be good to go.

    Update the controller method too so you're not doing it twice:

    @s3_direct_post = S3_BUCKET.presigned_post(
                      key: "uploads/#{SecureRandom.uuid}/${filename}",
                      success_action_status: 201,
                      acl: :public_read).where(:content_type).starts_with("")
    
    0 讨论(0)
  • 2020-12-16 17:07

    I believe the most current syntax for aws-sdk (2.9.6) is:

    @s3_direct_post = S3_BUCKET.presigned_post(
                      key: "uploads/#{SecureRandom.uuid}/${filename}",
                      success_action_status: 201,
                      acl: 'public-read',
                      content_type_starts_with: '')
    
    0 讨论(0)
  • 2020-12-16 17:12

    If anyone is looking for an updated answer to this question, I was able to solve this by:

    1. Adding a 'content type starts with' clause to my s3 presigned post, as documented on this page: http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
    2. Adding a hidden input named 'Content-Type' to my form and then updating it accordingly whenever a file is selected.
    0 讨论(0)
提交回复
热议问题