麻烦的设置内容类型在Amazon S3上使用carrierwave直接 [英] trouble setting content-type on amazon s3 with carrierwave direct

查看:172
本文介绍了麻烦的设置内容类型在Amazon S3上使用carrierwave直接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails应用程序,成功上传产品图片使用carrierwave和雾继railscast教程S3。现在,我尝试使用Carrierwave直接创业板直接上传文件到S3。这个工程,以及,除了内容类型设置为二进制/八位字节流,这使得图像在浏览器中调用只显示JPG格式的文件下载,而不是。

所以每个创业板的指示在 https://github.com/dwilkie/carrierwave_direct ,我加到carrierwave配置:

  config.will_include_content_type =真
 

和添加一个隐藏字段内容类型与为image / jpeg的价值。

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

 <形式接收字符集=utf-8行动=htt​​ps://mybucket.s3.amazonaws.com/级=new_product_image_uploaderENCTYPE =多部分/表单数据ID =new_product_image_uploader方法=邮报>
    < D​​IV的风格=保证金:0;填充:0;显示:内联><输入名称=UTF8类型=隐藏值=✓>< / DIV>
    <输入ID =product_image_uploader_keyNAME =键式=隐藏值=上传/.../$ {文件名}>
    <输入ID =product_image_uploader_aws_access_key_idNAME =AWSAccessKeyId类型=隐藏值=...>
    <输入ID =product_image_uploader_aclNAME =以acl类型=隐藏值=大众阅读>
    <输入ID =product_image_uploader_success_action_redirectNAME =success_action_redirect类型=隐藏值=HTTP://本地主机:5000 /产品>
    <输入ID =product_image_uploader_policyNAME =政策类型=隐藏值=...>
    <输入ID =product_image_uploader_signatureNAME =签名类型=隐藏值=...>
    <输入ID =product_image_uploader_product_imageNAME =文件类型=文件>
    <输入ID =Content-Type的NAME =Content-Type的类型=隐藏值=图像/ JPEG>< BR>
    <输入名称=提交类型=提交值=创建产品图片上传>
  < /形式GT;
 

该基地64 EN codeD方针是:

<$p$p><$c$c>{"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:这似乎与本文这儿对应

但是,当我在本地测试它,我得到浏览器,这个错误响应:

 &LT;错误&GT;
&LT; code取代; AccessDenied&LT; / code取代;
&LT;消息&gt;根据政策无效:政策条件失败:
[开始,以,$内容类型,]&LT; /消息&GT;
...
&LT; /错误&GT;
 

我真的不知道这意味着什么。我用Google搜索这个错误,发现这个问题: 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)

解决方案

谷歌上搜索周围的一些后,我发现这个页面上以下注释:的 http://doc.s3.amazonaws.com/proposals/post.html

 &LT;! - 该文件必须在请求中的最后一个有意义的元素;在此之后的其他元素将被忽略 - &GT;
 

切换内容类型隐藏的输入字段中输入文件输入字段解决了我的问题了。

I have a rails app that successfully uploads product images to s3 using carrierwave and fog following the railscast tutorial. Now I'm trying to use Carrierwave direct gem to upload the file directly to s3. This works as well, except the content-type was set as binary/octet-stream which makes the images invoke a file download in the browser instead of just displaying the jpg.

So per the gem instructions at https://github.com/dwilkie/carrierwave_direct, I added to carrierwave config:

config.will_include_content_type = true 

And added a hidden field Content-Type with value of image/jpeg.

The file upload form generated by the form helper seems to be correct:

  <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>

The base 64 encoded policy is:

{"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]]}

Which appears to correspond with this article here: http://aws.amazon.com/articles/1434

But when I test it locally I get this error response in the browser:

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

I don't really know what that means. I googled for this error and found this issue: https://github.com/dwilkie/carrierwave_direct/issues/63

But the solution is not helpful. Am I missing something?

I'm using:

carrierwave (0.8.0) activemodel (>= 3.2.0) activesupport (>= 3.2.0) carrierwave_direct (0.0.12)

rails (3.2.13)

解决方案

After googling around some more, I found the following comment on this page: 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 -->

Switching the Content-Type hidden input field before the file input field fixes my issue.

这篇关于麻烦的设置内容类型在Amazon S3上使用carrierwave直接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆