Tim*_*Tim 5 upload file-upload ruby-on-rails amazon-s3 amazon-web-services
我设置了我的 Rails 应用程序,每个用户都可以上传头像。图像使用 HTTP Post 直接上传到 Amazon 的 S3。到目前为止,除了用户能够上传任何类型的文件之外,一切正常。
我正在使用camelpunch 的d2s3 插件的一个分支。以下是帮手:
policy = Base64.encode64(
"{'expiration': '#{expiration_date}',
'conditions': [
{'bucket': '#{bucket}'},
['starts-with', '$key', '#{key}'],
{'acl': '#{acl}'},
{'success_action_redirect': '#{redirect}'},
['starts-with', '$Content-Type', '#{content_type}'],
['content-length-range', #{min_filesize}, #{max_filesize}]
]
}").gsub(/\n|\r/, '')
signature = b64_hmac_sha1(D2S3::S3Config.secret_access_key, policy)
out = ""
out << %(
<form action="https://#{bucket}.s3.amazonaws.com/" method="post" enctype="multipart/form-data" id="#{options[:form][:id]}" class="#{options[:form][:class]}">
<div>
<input type="hidden" name="key" value="#{key}/${filename}" />
<input type="hidden" name="AWSAccessKeyId" value="#{access_key_id}" />
<input type="hidden" name="acl" value="#{acl}" />
<input type="hidden" name="success_action_redirect" value="#{redirect}" />
<input type="hidden" name="policy" value="#{policy}" />
<input type="hidden" name="signature" value="#{signature}" />
<input type="hidden" name="Content-Type" value="#{content_type}" />
<input name="file" type="file" />#{submit_button}
</div>
</form>
)
Run Code Online (Sandbox Code Playgroud)
我的内容类型设置为“image/jpeg”,但亚马逊似乎忽略了这一点。在文档中,它说要像这样设置它。难道我做错了什么?
此外,当上传大于设置的最大文件大小的文件时,应用程序就会停止。S3 不会回复 xml 错误消息。这可以解决吗?
谢谢!
蒂姆
使用 AWS::S3 gem http://amazon.rubyforge.org/或 RightScale 的 RightAWS gem https://github.com/rightscale/right_aws会更容易吗?
| 归档时间: |
|
| 查看次数: |
2475 次 |
| 最近记录: |