I am trying to upload Images to aws s3 bucket. but i got unsupportedMediaType 415 error in postman. Not getting, where Its going wrong. Could you help me out? postman formdata request => taking file, fileName, and Id
In service.js file:
const aws = require('aws-sdk');
const fs = require('fs');
const bucketName = process.env.bucketName;
// Initiating S3 instance
const s3 = new aws.S3({
secretAccessKey: process.env.bucketSecretAccessKey,
accessKeyId: process.env.bucketAccessKeyId,
region: process.env.region
});
// Options you can choose to set to accept files upto certain size limit
// const options = {partSize: 10 * 1024 * 1024, queueSize: 1};
module.exports = {
upload: async function(payload) {
let fileContent = fs.readFileSync(payload.file)
const params = {
Bucket: bucketName,
ContentType: 'image/jpeg',
Key: payload.client_id/payload.familyName/payload.fileName,
Body: fileContent
};
let fileResp = null;
await s3.upload(params, function(err, data) {
if (err) {
console.log(err)
throw err;
}
fileResp = data;
console.log(`File uploaded successfully. ${data.Location}`);
});
return fileResp;
},
};
I am trying to upload Images to aws s3 bucket. but i got unsupportedMediaType 415 error in postman. Not getting, where Its going wrong. Could you help me out? postman formdata request => taking file, fileName, and Id
Api call through url:
uploadImage : async function(req, h) {
try{
let responseFile = null;
const uploadImage = await s3.upload(req.payload).then( async (resp) => {
responseFile = {fileUrl: resp.Location};
if(!!responseFile){
const updateFileName = await Family.updateOne({_id: req.payload.id},{
image: req.payload.fileName,
fileLocation : resp.Location
});
console.log(updateFileName)
}
}).catch((err) => {
console.log(err)
responseFile = err.message;
});
} catch (error) {
console.error(error.message);
return h.response({ error: responseMessages.internal_server_error }).code(500);
}
},
})
``````````````````````````````````````````
postman response:
`````````````````````
{
"statusCode": 415,
"error": "Unsupported Media Type",
"message": "Unsupported Media Type"
}
[1]: https://i.stack.imgur.com/ggHCj.png
CodePudding user response:
Use below
const params = {
Bucket: bucketName,
Key: `${payload.client_id}/${payload.familyName}/${payload.fileName}`,
Body: fileContent
};
There are two issues
- Key. path is not correct
- Content-type is not required
follow above snippet and try
CodePudding user response:
payload: { output: 'stream', parse: true, allow: 'multipart/form-data', multipart: true, timeout: false, },
I have added this payload in my route.js file, then it worked for me.