Presigned POST File Submission Invalid JSON

0

Hello there,

I am trying to send data from my website to my S3 Bucket with pre-signed POST. I was pretty sure I created my form-data correctly but error returned was:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>InvalidPolicyDocument</Code>
<Message>Invalid Policy: Invalid JSON.</Message>
<RequestId>0D3E83A53CC40B0D</RequestId><HostId>2DjNg4vUUOrP21XFT4ves6I8vS/4FM3APeMQStFnPX96wnciMCyONzgsG4RgWpb77DKopC/812Y=</HostId>
</Error
>```

Here's my back-end code that generates the pre-signed POST's required form data:

/**

  • Get Signed POST form data for uploading medias */ router.get('/postdata', function (req, res, next) { let postdata; let id = uuid(); s3.createPresignedPost({ Bucket: 'bucket', Conditions: [ { "bucket": "bucket" }, ["eq", "$key", 'file'] ["content-length-range", 1, 1024 * 1024 * 15], {"success_action_status": "200"} ], Expires: 300 }, (err, data) => { data.fields.key = 'file' data.fields.name = id; data.fields.success_action_status = "200"; postdata = data; });

    res.json(postdata); });


And here's how I generated the FormData object:

let form = new FormData(); Object.keys(result.fields).forEach(key => { form.append(key, result.fields[key]); });

form.append('file', document.getElementById('file').value);


The request body is also properly being encoded as multipart/form-data.   
  
So my question is, what part of this is "Invalid JSON". Also, what I don't understand even more is I have a working example but this one does just does not work anyway and anyhow I tried it.
ionizer
질문됨 6년 전1800회 조회
2개 답변
0

OK so I just realized that I needed to include the Content-Type header with the request.

fetch(action, {
    method: "POST",
    headers: {
        "Content-Type": "...."
    },
    body: form
})

But when I set my Content-Type to "multipart/form-data", I'm getting this error:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>MalformedPOSTRequest</Code>
<Message>The body of your POST request is not well-formed multipart/form-data.</Message>
<RequestId>7F543E78F106B3A4</RequestId>
<HostId>YV+rDi49ovxQJhZ9cGAWAxWwHz4g8xBS55gaD/tPN4EgAvrkAnMSbXiXL5m3g9xi+BvE8Rj76fw=</HostId>
</Error
>```

And when I set it to undefined, null or false like what I've read from many sources, I get this:

<?xml version="1.0" encoding="UTF-8"?> <Error> <Code>PreconditionFailed</Code> <Message>At least one of the pre-conditions you specified did not hold</Message><Condition>Bucket POST must be of the enclosure-type multipart/form-data</Condition> <RequestId>E2993D140D291888</RequestId> <HostId>+HZFLQbfjAWBQ7SY8FXWh4hUarRr2YefjOENWH/Eh7XZzuxyfa5DMy1didMZWhPA14XW3ql47iQ=</HostId> </Error >```

Also if I did not specify the Content-Type, I will go back to receiving Invalid JSON error like before

EDIT: Actually, not setting Content-Type leaves my browser to set the correct Content-Type and its boundary. But I'm still getting Invalid JSON error...

ionizer
답변함 6년 전
0

Ok, sorry. Silly me.

Conditions: [
    { "bucket": "bucket" },
    ["eq", "$key", 'file'] (<-------- MISSING COMMA)
    ["content-length-range", 1, 1024 * 1024 * 15],
    {"success_action_status": "200"}
]

Problem solved.

Edited by: ionizer on Nov 14, 2017 1:30 AM

ionizer
답변함 6년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠