Am I doing something wrong with POST uploads or is this a bug? #19727
-
[EDIT] Using the superagent library as described in the docs fixes this... Will post the answer once I find what it is but the catch is in the .attach() method After looking into the superagent library, it would appear that .attach() is just a replacement for formData.append() with the same syntax, but my quick reproduction with the .append() method doesn't work... Will post info after I explore it deeper. POST uploads failI am trying to solve my problem with PresignedPutUrls (which work perfectly) but cannot enforce any restrictions as PresignedPostUrls can - e.g. file size. However, no matter what I do I get the following error: For reference, my bucket's name is Creating policyconst postPolicy = minioClient.newPostPolicy();
postPolicy.setBucket("tasky-app");
// tried with both .setKey() and .setKeyStartsWith(), but get same error
//postPolicy.setKey("somenameidk.png");
postPolicy.setKeyStartsWith("profile-images/somenameidk.png");
postPolicy.setExpires(new Date(Date.now() + 60 * 60 * 24));
postPolicy.setContentType("text/plain");
postPolicy.setContentDisposition("attachment; filename=example.png");
// 1KB - 10MB
postPolicy.setContentLengthRange(1024, 1024 * 1024 * 100);
postPolicy.setUserMetaData({
key: "value",
smoke: "data",
});
const presignedPost = await minioClient.presignedPostPolicy(postPolicy);
console.log(presignedPost); After which I get: {
postURL: 'http://localhost:8001/tasky-app',
formData: {
bucket: 'tasky-app',
key: 'profile-images/somenameidk.png',
'Content-Type': 'image/png',
'Content-Disposition': 'attachment; filename=example.png',
'x-amz-meta-key': 'value',
'x-amz-meta-smoke': 'data',
'x-amz-date': '20240513T103640Z',
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
'x-amz-credential': 'JJtetbdFP9nyGrG4x7iQ/20240513/us-east-1/s3/aws4_request',
policy: 'eyJjb25kaXRpb25zIjpbWyJlcSIsIiRidWNrZXQiLCJ0YXNreS1hcHAiXSxbInN0YXJ0cy13aXRoIiwiJGtleSIsInByb2ZpbGUtaW1hZ2VzL3NvbWVuYW1laWRrLnBuZyJdLFsiZXEiLCIkQ29udGVudC1UeXBlIiwidGV4dC9wbGFpbiJdLFsiZXEiLCIkQ29udGVudC1EaXNwb3NpdGlvbiIsImF0dGFjaG1lbnQ7IGZpbGVuYW1lPWV4YW1wbGUucG5nIl0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMTAyNCwxMDQ4NTc2MDBdLFsiZXEiLCIkeC1hbXotbWV0YS1rZXkiLCJ2YWx1ZSJdLFsiZXEiLCIkeC1hbXotbWV0YS1zbW9rZSIsImRhdGEiXSxbImVxIiwiJHgtYW16LWRhdGUiLCIyMDI0MDUxM1QxMDM2NDBaIl0sWyJlcSIsIiR4LWFtei1hbGdvcml0aG0iLCJBV1M0LUhNQUMtU0hBMjU2Il0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwiSkp0ZXRiZEZQOW55R3JHNHg3aVEvMjAyNDA1MTMvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJdXSwiZXhwaXJhdGlvbiI6IjIwMjQtMDUtMTNUMTA6Mzg6MDYuNjczWiJ9',
'x-amz-signature': '90c4bdaf683fb13e7dc7701b5a45d506e44e2fda693145a2c112fd48ca98d359'
}
} Uploading imageI have a simple form which only accespts one file and handles the upload. (Policies is copied here for simplicity) <form
name="myform"
encType="multipart/form-data"
method="POST"
onSubmit={async (e) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const postPolicy = {
postURL: 'http://localhost:8001/tasky-app',
formData: {
bucket: 'tasky-app',
key: 'profile-images/somenameidk.png',
'Content-Type': 'text/plain',
'Content-Disposition': 'attachment; filename=example.png',
'x-amz-meta-key': 'value',
'x-amz-meta-smoke': 'data',
'x-amz-date': '20240513T103640Z',
'x-amz-algorithm': 'AWS4-HMAC-SHA256',
'x-amz-credential': 'JJtetbdFP9nyGrG4x7iQ/20240513/us-east-1/s3/aws4_request',
policy: 'eyJjb25kaXRpb25zIjpbWyJlcSIsIiRidWNrZXQiLCJ0YXNreS1hcHAiXSxbInN0YXJ0cy13aXRoIiwiJGtleSIsInByb2ZpbGUtaW1hZ2VzL3NvbWVuYW1laWRrLnBuZyJdLFsiZXEiLCIkQ29udGVudC1UeXBlIiwidGV4dC9wbGFpbiJdLFsiZXEiLCIkQ29udGVudC1EaXNwb3NpdGlvbiIsImF0dGFjaG1lbnQ7IGZpbGVuYW1lPWV4YW1wbGUucG5nIl0sWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsMTAyNCwxMDQ4NTc2MDBdLFsiZXEiLCIkeC1hbXotbWV0YS1rZXkiLCJ2YWx1ZSJdLFsiZXEiLCIkeC1hbXotbWV0YS1zbW9rZSIsImRhdGEiXSxbImVxIiwiJHgtYW16LWRhdGUiLCIyMDI0MDUxM1QxMDM2NDBaIl0sWyJlcSIsIiR4LWFtei1hbGdvcml0aG0iLCJBV1M0LUhNQUMtU0hBMjU2Il0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwiSkp0ZXRiZEZQOW55R3JHNHg3aVEvMjAyNDA1MTMvdXMtZWFzdC0xL3MzL2F3czRfcmVxdWVzdCJdXSwiZXhwaXJhdGlvbiI6IjIwMjQtMDUtMTNUMTA6Mzg6MDYuNjczWiJ9',
'x-amz-signature': '90c4bdaf683fb13e7dc7701b5a45d506e44e2fda693145a2c112fd48ca98d359'
}
}
Object.keys(postPolicy.formData).forEach((key) => {
formData.append(key, postPolicy.formData[key]);
});
try {
const response = await fetch(postPolicy.postURL, {
method: "POST",
mode: "cors",
body: formData,
headers: {
"Access-Control-Allow-Origin": "*",
},
});
console.log("response", response);
} catch (err) {
console.log("UERRRR", err);
}
}}
>
<input type="file" name="file" />
<button className="border-4 bg-red-500 text-white" type="submit">
Submit
</button>
</form> ErrorsMinIO Trace
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I am not a JS expert at any level, but My guess is that |
Beta Was this translation helpful? Give feedback.
-
I found the solution to this problem, although I am still not sure why it works like that... I compared the request the superagent library made vs my own XHR and/or fetch request. The only difference I noticed was the order of the properties in the formData part of the request. Specifically, in my own requests the However, when I put the In order, it's the superagent library XHR request, then my Fetch request followed by my own XHR. I don't know if this is intended behaviour with XHR or something but it really took me some time to realize 😅 |
Beta Was this translation helpful? Give feedback.
I am not a JS expert at any level, but
key: 'profile-images/somenameidk.png',
is no coming through.My guess is that
Object.keys(postPolicy.formData).forEach((key)
never runs.