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
gefragt vor 6 Jahren1839 Aufrufe
2 Antworten
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
beantwortet vor 6 Jahren
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
beantwortet vor 6 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen