S3预签名URL:根据策略无效:策略条件失败success_action_status

时间:2019-04-12 23:53:08

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

我试图在服务器上发布预签名的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)
  });

同样,当我在success_action_status中离开presigned_post字段时,它仍然有效。但是当我不明白的时候:

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

任何人都知道发生了什么事吗?谢谢!

解决方案:

formData.append("file", that.file[0].files[0]);必须是附加在表单上的最后

1 个答案:

答案 0 :(得分: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)
  });