Reputation: 2821
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.
Upvotes: 1
Views: 464
Reputation: 2821
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", ""]
Upvotes: 1