I have a Capacitor application, which I am taking an Image from the camera, and uploading as a Blob to S3.
When using the “web” platform and thus the “webPath”, the upload works fine.
When using a device (Android in my case) and converting the “filePath” to a Blob, the Blob won’t upload, with AWS returning an error.
Is there something wrong with the Blob I am creating from the “filePath”?
export async function uploadImageToAws(fileUri, fileName) {
const blob = fileUri.startsWith('blob')
? await convertImageWebPathToBlob(fileUri)
: await convertImagePathToBlob(fileUri);
const formData = new FormData();
formData.append('bucket', s3URI);
formData.append('key', fileName);
formData.append('Content-Type', 'image/jpeg');
formData.append('AWSAccessKeyId', awsKey);
formData.append('Policy', policyBase64);
formData.append('Signature', signature);
formData.append('file', blob, fileName);
const options = {url: s3URI, data: formData};
return await CapacitorHttp.post(options);
}
async function convertImageWebPathToBlob(imageWebPath) {
const response = await fetch(imageWebPath);
return await response.blob();
}
async function convertImagePathToBlob(imagePath) {
var file = await Filesystem.readFile({ path: imagePath })
const rawData = atob(file.data);
const bytes = new Array(rawData.length);
for (let x = 0; x < rawData.length; x++) {
bytes[x] = rawData.charCodeAt(x);
}
const arr = new Uint8Array(bytes);
return new Blob([arr], { type: 'image/jpeg' });
}
Again, the Blob created from “convertImageWebPathToBlob” uploads perfectly.
The Bloc created from “convertImagePathToBlob” does not upload, and Amazon returns the error:
<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>J5MS6JMB128DJQ</RequestId>
<HostId>Hpp1bKq104X24wwkwplS6dUNg1/G17LoJfPT5+/Sudg/sXni2q/rI=</HostId>
</Error>