【发布时间】: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