我试图在服务器上发布预签名的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]);
必须是附加在表单上的最后。
答案 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)
});