【问题标题】:Can't set content type when uploading to Amazon S3上传到 Amazon S3 时无法设置内容类型
【发布时间】:2014-11-05 17:45:24
【问题描述】:

我正在尝试在使用 jQuery 和 Rails 上传到 s3 目录时设置文件的内容类型。我已经成功实现Ryan Bate's solution 没有问题。

问题是内容类型未设置,默认为binary/octet-stream。我在 Ryan 的解决方案中添加了以下内容:

uploader_helper.rb

def fields
  {
    # ...
    :content_type => nil,
    # ...
  }
end

paintings.js.coffee

$("#fileupload").fileupload
  add: (e, data) ->
    # ...
    if types.test(file.type) || types.test(file.name)
      # ...
      data.form.find('#content_type').attr('name','Content-Type')
      data.form.find('#content_type').val(file.type)
      data.submit()
    # ...

此外,我的 CORS 配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>http://localhost:3000</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

我在尝试添加这些字段时收到403 (Forbidden) 错误。

我尝试添加存储桶策略,但没有帮助。

【问题讨论】:

    标签: ruby-on-rails amazon-s3 jquery-file-upload


    【解决方案1】:

    Ryan's tutorial 之后,需要将有关内容类型的策略添加到policy_data 方法中。

    例如,如果我们要允许任何内容类型,则应添加此行:

    ["starts-with", "$Content-Type", ""]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 2011-04-27
      • 2013-12-07
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2012-04-25
      • 2020-05-02
      相关资源
      最近更新 更多