Experts,
I have a JSON object that I need to save to the S3 bucket as a CSV file. This is what I have managed so far but unfortunately, the file is not getting created on S3 and no error is reported.
const AWS = require('aws-sdk');
const converter = require('json-2-csv');
const s3 = new AWS.S3({
accessKeyId: "Key",
secretAccessKey: "Secret"
});
exports.handler = async(event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const s3Bucket = "bucketname";
const objectName = event.operation + new Date().getTime() + "_" + event.userId + ".";
const objectData = (event.rawData);
const objectType = "text/csv";
converter.json2csv(objectData, async(err, csv) => {
if (err) {
console.log(" Error ", err)
throw err;
}
console.log(csv);
try {
const params = {
Bucket: s3Bucket,
Key: objectName,
Body: (csv),
ContentType: objectType,
ContentDisposition: 'attachment',
};
console.log('Writing to s3 bucket..');
const result = await s3.putObject(params).promise();
console.log('Finished Writing to s3 bucket..', objectName);
return sendRes(200, `File uploaded successfully at https:/` + s3Bucket + `.s3.amazonaws.com/` + objectName);
}
catch (error) {
console.log(error)
return sendRes(404, error);
}
});
};
const sendRes = (status, body) => {
var response = {
statusCode: status,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods": "OPTIONS,POST,PUT",
"Access-Control-Allow-Credentials": true,
"Access-Control-Allow-Origin": "*",
"X-Requested-With": "*"
},
body: body
};
return response;
};
If I remove the JSON to CSV converter and try saving the JSON data as a JSON file then it works with a charm - by changing the objectType to "application/json".
What am I doing wrong here when converting to CSV ?
P.S: JSON to CSV conversion works fine and this is the last printed log.
Writing to s3 bucket..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…