2

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

因此,根据https://github.com/dwilkie/carrierwave_direct上的 gem 说明,我添加到 carrierwave 配置中:

config.will_include_content_type = true 

并添加了一个值为 image/jpeg 的隐藏字段 Content-Type。

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

  <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 (>= 3.2.0) activesupport (>= 3.2.0) carrierwave_direct (0.0.12)

导轨 (3.2.13)

4

1 回答 1

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 隐藏输入字段。

于 2013-06-21T06:11:14.570 回答