【问题标题】:S3 Presigned URLs: Invalid according to Policy: Policy Condition failed success_action_statusS3 预签名 URL:根据策略无效:策略条件失败 success_action_status
【发布时间】:2019-09-03 18:12:10
【问题描述】:

我正在尝试通过我的服务器发布预签名 URL,然后在浏览器中通过 javascript 上传。当我省略 :success_action_status 字段时一切正常,但我想将其设置为 201 以在上传后取回 XML。

在服务器上:

s3_bucket = Aws::S3::Resource.new.bucket(UploadFile::DECK_BUCKET)
presigned_url = s3_bucket.presigned_post(
  :key => @upload_file.key,
  :content_length_range => 1..(10*1024),
  :success_action_status => '201',
  :signature_expiration => expire
)

data = { url: presigned_url.url, url_fields: presigned_url.fields }
render json: data, status: :ok

在客户端:

this.file.change(function() {
  var formData = new FormData();
  formData.append("key", that.fields.key);
  formData.append("X-Amz-Credential", that.fields['x-amz-credential']);
  formData.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
  formData.append("X-Amz-Date", that.fields['x-amz-date']);
  formData.append("Policy", that.fields.policy);
  formData.append("X-Amz-Signature", that.fields['x-amz-signature']);
  formData.append("file", that.file[0].files[0]);
  formData.append("success_action_status", that.fields['success_action_status']);
  that.$http.post(that.url, formData).then(function(response) {
    console.log("yup")
    console.log(response)
  }, function(response) {
    console.log("nope")
    console.log(response)
  });

当我离开presigned_post 中的success_action_status 字段时,它再次起作用。但是当我没有得到时:

Invalid according to Policy: Policy Condition failed: ["eq", "$success_action_status", "201"]

谁知道怎么回事??谢谢!

解决方案:

formData.append("file", that.file[0].files[0]); 必须是最后一个附加到表单的东西。

【问题讨论】:

    标签: ruby-on-rails ruby amazon-web-services amazon-s3 aws-sdk


    【解决方案1】:

    the documentation 中似乎没有具体说明为什么这不起作用。

    更新

    尝试将success_action_status 字段放在文件字段之前

    this.file.change(function() {
      var formData = new FormData();
      formData.append("key", that.fields.key);
      formData.append("X-Amz-Credential", that.fields['x-amz-credential']);
      formData.append("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
      formData.append("X-Amz-Date", that.fields['x-amz-date']);
      formData.append("Policy", that.fields.policy);
      formData.append("X-Amz-Signature", that.fields['x-amz-signature']);
      formData.append("success_action_status", that.fields['success_action_status']);
      formData.append("file", that.file[0].files[0]);
      that.$http.post(that.url, formData).then(function(response) {
        console.log("yup")
        console.log(response)
      }, function(response) {
        console.log("nope")
        console.log(response)
      });
    

    【讨论】:

    • 它不起作用的原因正是因为缺少formData.append("success_action_status",...。该策略指定并限制表单必须包含的内容,因此如果没有这个,表单确实是“根据策略无效”。
    • 我添加了formData.append("success_action_status", '201') 仍然是同样的错误。我尝试了整数和字符串形式的 201。如果我在 ruby​​ 代码中使用 201 作为整数,则会出现错误。
    • @Michael-sqlbot 你能在这里提供一些建议吗?
    • @frausto formData.append("file", ... 必须是 last 附加到表单的内容。在上面添加success_action_status,而不是下面。
    • @Michael-sqlbot 就是这样!感谢您的回答!
    猜你喜欢
    • 2013-12-29
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多