Reputation: 11
exports.FilelamdaFunctionHandler =async function( event, context, callback){
try {
let p = new Promise(function(resolve,reject){
let contentType = event.headers['Content-Type']
console.log('content type is--' + contentType)
contentType = contentType.split("/")[1]
let type = (contentType == 'plain') ? ".txt" : ".csv"
const params = {
Body: event.body,
Bucket: bucketName + "/" + event.pathParameters.hospital,
Key: event.pathParameters.hospital + type,
ContentType: "*"
};
try {
s3.putObject(params, (err, results) => {
if (err) {
console.log('errors in promise' + err)
reject(err)
}
else {
console.log('resultss --' + JSON.stringify(results))
resolve(results)
}
})
} catch (error) {
console.log('e--' + error.toString())
console.log('errorororooror' + JSON.stringify(error))
}
});
console.log('second')
p.then((result)=>{
const response={
'statusCode':200,
'body':"sucess"
}
callback(null,response)
}).catch((error)=>{
const response={
'statusCode':500,
'body':"error"
}
callback(null,response)
})
} catch (e) {
console.log('e--' + e.toString())
console.log('errr' + JSON.stringify(e))
}
}
I am using a promise to upload the data to the s3 bucket but the promise does not return anything, could any help me out
Upvotes: 0
Views: 49
Reputation: 664538
Avoid using new Promise
when aws-sdk already supports promises natively, do not take a callback
argument to an async function
that returns a promise, and don't use then
/catch
when you can use await
for the same:
exports.FilelamdaFunctionHandler = async function(event, context){
try {
let contentType = event.headers['Content-Type']
console.log('content type is--' + contentType)
contentType = contentType.split("/")[1]
let type = (contentType == 'plain') ? ".txt" : ".csv"
const params = {
Body: event.body,
Bucket: bucketName + "/" + event.pathParameters.hospital,
Key: event.pathParameters.hospital + type,
ContentType: "*"
};
const results = await s3.putObject(params).promise();
console.log('resultss --' + JSON.stringify(results);
} catch(error) {
console.log('errors in promise' + error)
console.log('e--' + error.toString())
console.log('errorororooror' + JSON.stringify(error));
const response = {
'statusCode':500,
'body':"error"
}
return response;
}
console.log('second')
const response = {
'statusCode':200,
'body':"sucess"
}
return response;
}
Upvotes: 1
Reputation: 11105
You are not awaiting p
. Lambda will simply kill the session if you do not await your promises.
...
p.then((result)=>{
const response={
'statusCode':200,
'body':"sucess"
}
callback(null,response)
}).catch((error)=>{
const response={
'statusCode':500,
'body':"error"
}
callback(null,response)
})
await p
} catch (e) {
console.log('e--' + e.toString())
console.log('errr' + JSON.stringify(e))
}
Upvotes: 0