简体   繁体   中英

Can't set content type when uploading to Amazon S3

I'm attempting to set the content type of a file when uploading to s3 directory with jQuery and Rails. I've successfully implemented Ryan Bate's solution without issue.

The problem is the content type does not get set and defaults to binary/octet-stream . I've added into Ryan's solution the following:

uploader_helper.rb

def fields
  {
    # ...
    :content_type => nil,
    # ...
  }
end

paintings.js.coffee

$("#fileupload").fileupload
  add: (e, data) ->
    # ...
    if types.test(file.type) || types.test(file.name)
      # ...
      data.form.find('#content_type').attr('name','Content-Type')
      data.form.find('#content_type').val(file.type)
      data.submit()
    # ...

In addition, my CORS config looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:3000</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

I receive a 403 (Forbidden) error when I try adding these fields.

I've tried adding a bucket policy, but it didn't help.

Following Ryan's tutorial , a policy on content type needs to be added to the policy_data method.

For example, if we want to allow any content-type, then this line should be added:

["starts-with", "$Content-Type", ""]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM