I’m trying to update a local JPG image file into an S3 bucket using the REST PUT request and Axios.
I managed to send the PUT request and to get a positive answer from AWS S3 Service but what it’s been upload is not a JPG file but a JSON file.
This is the code that I’m using:
//Create the FormData var data = new FormData(); data.append('file', fs.createReadStream(image_path)); //Send the file to File-system console.log("Sending file to S3..."); const axiosResponse = await axios.put(image_signed_url, { data: data, headers: { 'Content-Type': 'multipart/form-data' } }).catch(function(error) { console.log(JSON.stringify(error)); return null; });
I have already try to change the headers to {'Content-Type': 'application/octet-stream' }
but I obtained the same result.
Advertisement
Answer
It did not manage to make AXIOS work in order to upload an image.
The node-fetch module did it sending the image as a binary and specifying the “Content-type”.
If I try to the same using AXIOS it the image was always packet into a form-data and the result was JSON file uploaded into the S3 bucket instead of the image.
//Send the file to File-system console.log("Sending file to S3..."); const resp = await fetch(image_signed_url, { method: 'PUT', body: fs.readFileSync(image_path), headers: { 'Content-Type': 'image/jpeg', }, }).catch( err => { console.log(err); return null; });