【问题标题】:Amazon S3 direct upload not recognizing file's content typeAmazon S3 直接上传无法识别文件的内容类型
【发布时间】:2011-04-27 08:38:06
【问题描述】:

我设置了我的 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>
    )

我的内容类型设置为“图像/JPEG”,但亚马逊似乎忽略了这一点。在文档中,它说要像这样设置它。我做错了吗?

此外,当上传大于设置的最大文件大小的文件时,应用程序会停止。 S3 不回复 xml 错误消息。可以解决吗?

谢谢!

提姆

【问题讨论】:

  • 您能否澄清一下:您是否尝试通过设置内容类型来执行验证?

标签: ruby-on-rails file-upload upload amazon-s3 amazon-web-services


【解决方案1】:

Amazon 无法确定文件的实际内容类型,因为这需要分析二进制数据。

亚马逊使用上传时声明的内容类型提供文件,因此该政策仅限制用户上传将使用不允许的内容类型提供的文件。例如,没有人可以破解您的表单并将某些内容用作“应用程序/pdf”。但是,他们可以上传声称它是“图像/jpeg”的 PDF 文件,该文件将作为 jpeg 提供给消费者,从而导致图像损坏。

【讨论】:

    【解决方案2】:

    使用 AWS::S3 gem http://amazon.rubyforge.org/ 或 RightScale 的 RightAWS gem https://github.com/rightscale/right_aws 会更容易吗?

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多