I have written a lambda function to copy a file to another s3 bucket and delete the object from current bucket, but it is stooped at some line and not executed after that. I tried below two options, first one stops at a line second one finishes but response is not coming to API gateway proxy
var AWS = require("aws-sdk");
// Main Lambda entry point
exports.handler = async (event) => {
await getUploadURL(event);
var responseBody = {
"status": "success",
"message": "The file was successfully moved"
};
const response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
return response;
};
const getUploadURL = async function(event) {
console.log("invoking lambda starting");
var s3 = new AWS.S3();
var sourceBucket = "sourcebucket";
var destinationBucket = "destbucket";
var objectKey = event.queryStringParameters.fileName;
console.log("Object Key", objectKey);
//Only execute till here after that no execution log
var copySource = encodeURI(sourceBucket + "/" + objectKey);
var copyParams = {
Bucket: destinationBucket,
CopySource: copySource,
Key: objectKey
};
var deleteParams = {
Bucket: sourceBucket,
Key: objectKey
};
/** var responseBody = {
"status": "success",
"message": "The file was successfully marked as read"
};
var failedResponseBody = {
"status": "Failed",
"message": "The file was not copied"
};**/
await s3.copyObject(copyParams, function (err, data) {
console.log("Starting Copying function");
if (err) {
console.log("error occured during copy", err, err.stack);
} else {
console.log("The copy was successful, response");
}
});
await s3.deleteObject(deleteParams, function (err, data) {
console.log("Deleting the file from S3");
if (err)
console.log(err, err.stack); // error
else
console.log("Deleted the File from S3"); // deleted
});
};
I tried another option with call back, it is working but the lambda is not returning proper response. The file is moved but the response to API gateway is coming as null. The API is complaining with 502 for invalid response
var AWS = require("aws-sdk");
exports.handler = (event, context, callback) => {
console.log("invoking lambda starting")
var s3 = new AWS.S3();
var sourceBucket = "source";
var destinationBucket = "destination";
var objectKey = event.queryStringParameters.fileName
console.log("Object Key", objectKey)
var copySource = encodeURI(sourceBucket + "/" + objectKey);
var copyParams = {
Bucket: destinationBucket,
CopySource: copySource,
Key: objectKey
};
var deleteParams = {
Bucket: sourceBucket,
Key: objectKey
}
var responseBody = {
"status": "success",
"message": "The file was successfully marked as read"
};
var failedResponseBody = {
"status": "Failed",
"message": "The file was not copied"
};
s3.copyObject(copyParams, function (err, data) {
console.log("Starting Copying function");
if (err) {
console.log("error occured during copy", err, err.stack);
} else {
console.log("The copy was successful, response", response);
}
});
s3.deleteObject(deleteParams, function (err, data) {
console.log("Deleting the file from S3");
if (err)
console.log(err, err.stack); // error
else
console.log("Deleted the File from S3"); // deleted
});
const response = {
"statusCode": 200,
"headers": {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE"
},
"body": JSON.stringify(responseBody),
"isBase64Encoded": false
};
return callback(response);
};