使用carrierwave direct在亚马逊s3上设置内容类型时出现问题

时间:2013-06-21 04:07:59

标签: ruby-on-rails-3 amazon-s3 carrierwave content-type

我有一个rails应用程序,可以在railscast教程后使用carrierwave和fog成功将产品图像上传到s3。现在我正在尝试使用Carrierwave direct gem将文件直接上传到s3。这也有效,除了内容类型被设置为二进制/八位字节流,这使得图像在浏览器中调用文件下载而不是仅显示jpg。

所以根据https://github.com/dwilkie/carrierwave_direct的gem说明,我添加到了carrierwave config:

config.will_include_content_type = true 

并添加了隐藏字段Content-Type,其值为image / jpeg。

表单助手生成的文件上传表单似乎是正确的:

  <form accept-charset="UTF-8" action="https://mybucket.s3.amazonaws.com/" class="new_product_image_uploader" enctype="multipart/form-data" id="new_product_image_uploader" method="post">
    <div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"></div>
    <input id="product_image_uploader_key" name="key" type="hidden" value="uploads/.../${filename}">
    <input id="product_image_uploader_aws_access_key_id" name="AWSAccessKeyId" type="hidden" value="...">
    <input id="product_image_uploader_acl" name="acl" type="hidden" value="public-read">
    <input id="product_image_uploader_success_action_redirect" name="success_action_redirect" type="hidden" value="http://localhost:5000/products">
    <input id="product_image_uploader_policy" name="policy" type="hidden" value="...">
    <input id="product_image_uploader_signature" name="signature" type="hidden" value="...">
    <input id="product_image_uploader_product_image" name="file" type="file">
    <input id="Content-Type" name="Content-Type" type="hidden" value="image/jpeg"><br>
    <input name="commit" type="submit" value="Create Product image uploader">
  </form>

base 64编码策略是:

{"expiration":"2013-06-21T13:35:42Z","conditions":[["starts-with","$utf8",""],["starts-with","$key","uploads"],["starts-with","$Content-Type",""],{"bucket":"mybucket"},{"acl":"public-read"},{"success_action_redirect":"http://localhost:5000/products"},["content-length-range",1,5242880]]}

这似乎与本文相对应:http://aws.amazon.com/articles/1434

但是当我在本地测试时,我在浏览器中收到此错误响应:

<Error>
<Code>AccessDenied</Code>
<Message>Invalid according to Policy: Policy Condition failed: 
["starts-with", "$Content-Type", ""]</Message>
...
</Error>

我真的不知道这意味着什么。我搜索了这个错误,发现了这个问题:https://github.com/dwilkie/carrierwave_direct/issues/63

但解决方案没有帮助。我错过了什么吗?

我正在使用:

carrierwave(0.8.0)       activemodel(&gt; = 3.2.0)       activesupport(&gt; = 3.2.0)     carrierwave_direct(0.0.12)

rails(3.2.13)

1 个答案:

答案 0 :(得分:1)

在搜索了更多内容后,我在此页面上发现了以下评论:http://doc.s3.amazonaws.com/proposals/post.html

<!-- The file must be the last meaningful element in the request; other elements after this will be ignored -->

在文件输入字段之前切换Content-Type隐藏输入字段可以解决我的问题。