waynehoover / s3_direct_upload

Direct Upload to Amazon S3 With CORS
MIT License
651 stars 333 forks source link

Policy Condition failed: ["starts-with", "$Content-Type", ""] error #29

Open mmc1ntyre opened 11 years ago

mmc1ntyre commented 11 years ago

Any ideas why this would be happening?

AccessDenied Invalid according to Policy: Policy Condition failed: ["starts-with", "$Content-Type", ""] D7C6C1FE8775221F 5qRPVAAM/6iZF0w8vfOlrm67FXoCiDZyDeZklt/lL2ndLb4+/Cry4JclqN/Zsw7L
pomartel commented 11 years ago

It's most likely a configuration bug. Double check your CORS policy on the S3 server. The declared server must match the one you are using.

mmc1ntyre commented 11 years ago

I went through the Railscasts tutorial again and added a coffee script file "similar" to the one at ....

https://github.com/railscasts/383-uploading-to-amazon-s3/blob/master/gallery-jquery-fileupload/app/assets/javascripts/paintings.js.coffee

Once I did this it worked. I'm not sure if gem was supposed to have this baked in or the documentation needs updating.

waynehoover commented 11 years ago

The Content-Type hidden field isn't being included in your form most likely. Did you use the form helper this gem provides or the one in the railscast?

mmc1ntyre commented 11 years ago

Thanks for everyone's help...the issue is fixed

peleteiro commented 11 years ago

I'm having this problem. How do I get this fix?

waynehoover commented 11 years ago

your getting this error? Invalid according to Policy: Policy Condition failed: ["starts-with", "$Content-Type", ""]

peleteiro commented 11 years ago

Yes

squareduck commented 11 years ago

Putting this into form resolves the error:

hidden_field_tag  "Content-type", ""

I have not found yet the reason why my S3 bucket expects this field. Apparently this demo works without this field: https://github.com/pjambet/direct-upload

ahoward commented 11 years ago

i am also seeing this

bhserna commented 11 years ago

I am also having this problem. The funky thing is that it was working fine.

waynehoover commented 11 years ago

Make sure you use the correct helper method in your view, something like:

<%= s3_uploader_form callback_url: model_url, callback_param: "model[image_url]", id: "myS3Uploader" do %>
  <%= file_field_tag :file, multiple: true %>
<% end %>

Are you doing this?

bhserna commented 11 years ago

Yes but without the callbacks

<%= s3_uploader_form id: "myS3Uploader" do %>
  <%= file_field_tag :file, multiple: true %>
<% end %>
waynehoover commented 11 years ago

When you look at the HTML this helper method generates the form tag should look like this:

<form accept-charset="UTF-8" action="https://s3.amazonaws.com/bucket-name" data-callback-method="POST" data-callback-param="file" data-callback-url="null" enctype="multipart/form-data" id="s3uploaderz" method="post">

...

</form>

Does yours? It seems that for some reason some browsers are not sending the content-type header when posting the form, I'm trying to track down why. In the mean time you can just add the hidden field like @danvelduck did like so:

<%= s3_uploader_form id: "myS3Uploader" do %>
  <%= hidden_field_tag  "Content-Type", "" %>
  <%= file_field_tag :file, multiple: true %>
<% end %>
bhserna commented 11 years ago

sorry, it was my problem ... in my assets I had a a different version of the "jquery.fileupload.js" ... I just remove that and now is working.

I think the problem was that this line https://github.com/waynehoover/s3_direct_upload/blob/master/app/assets/javascripts/s3_direct_upload.js.coffee#L90

was not doing its job.

Thanks, for your help

webbtj commented 11 years ago

I'm having the same issue. I followed the readme to set up. One difference is that I am loading my form via an ajax request, so I'm doing my $("#s3-uploader").S3Uploader(); in the new.js.erb for my image model instead of doing it in my application script (which seems to be working ok). I've added the hidden input field as suggested and also revised my CORS to match the example in the readme. Any ideas what else I could try or what kind of information would help find the issue?

Thanks for your help

david-a commented 10 years ago

I had the same issue, I was able to fix it by adding () to the S3Uploader function call jQuery("#fileS3Uploader").S3Uploader() instead of jQuery("#fileS3Uploader").S3Uploader