Tag: amazon s3

使用Rails和jQuery文件上载将Content-Type直接设置为S3上载

我在这个主题上看到过很多问题,但是我遇到过的任何问题都没有对我有用,所以我在这里张贴另一个…… 我正在使用Ruby on Rails尝试使用jQuery File Upload插件直接配置文件上传到Amazon S3。 我跟着非常有用的Heroku教程来完成初始设置。 文件上传很好,但在S3中它们都被标记为Content-Type: binary/octet-stream ,所以当它们在应用程序中提供时,所有文件都会下载而不是直接打开。 这是一个问题,因为我试图允许图像,PDF,音频或video文件,所以我需要能够从文件中获取正确的Content-Type并将其传递给S3。 在查看亚马逊上的AWS-SDK gem文档时,我看到了本节有关将.where(:content_type).starts_with(“”)到预先签名的post对象末尾以修改策略的内容。 但是,当我这样做时,它引发了一个错误: AccessDenied Invalid according to Policy: Policy Condition failed: [“starts-with”, “$Content-Type”, “”] 所以我将content_type: “”添加到预先签名的post对象的opts哈希中,现在它再次工作,但是所有文件默认为binary/octet-stream它们都默认为image/jpeg 。 这是我现在的代码: 调节器 def new @s3_direct_post = S3_BUCKET.presigned_post( key: “uploads/#{SecureRandom.uuid}/${filename}”, success_action_status: 201, acl: :public_read, content_type: “”).where(:content_type).starts_with(“”) end _form.html.haml :javascript $(function() { $(‘.directUpload’).find(“input:file”).each(function(i, elem) { var fileInput = […]