调用DynamoDB查询,然后将结果写入s3存储桶:如何使用async await简化此代码? [英] Calling DynamoDB query and then writing result to s3 bucket: How to simplify this code with async await?
本文介绍了调用DynamoDB查询,然后将结果写入s3存储桶:如何使用async await简化此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下lambda函数,该函数从DynamoDB表读取,然后将结果写入S3存储桶中的电子表格.
I have the following lambda function that reads from a DynamoDB table then writes the result to a spreadsheet on the S3 bucket.
它工作正常,但我想使用异步等待来改善它.这可能吗?
It works fine but I'd like to improve it using async await. Is this possible?
'use strict';
const AWS = require('aws-sdk');
const S3 = new AWS.S3();
const excel = require('excel4node');
const workbook = new excel.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10'});
const contactsTable = process.env.CONTACTS_TABLE;
const contactsBucket = process.env.CONTACTS_BUCKET;
const bucketRegion = process.env.REGION;
module.exports.exportContactsByID = (event, _context, callback) => {
const id = event.pathParameters.id;
const params = {
TableName: contactsTable,
IndexName: "company_id-created_at-index",
KeyConditionExpression: "company_id = :v_ID",
ExpressionAttributeValues: {
":v_ID": id,
},
ProjectionExpression: "company_name, contact_name, age, phone, email, dsr_ratio, max_eligibility, created_at_local, selected_banks"
};
return db.query(params).promise()
.then((res) => {
const items = res.Items;
if(res.Count === 0){
return callback(null, response(200, `No records found for id ${id}`));
}
// Headers + Columns
worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
worksheet.cell(1, 8).string('Submission Date').style({font: {bold: true}});
worksheet.cell(1, 9).string('Banks').style({font: {bold: true}});
// Rows
items.forEach((item, i) => {
worksheet.cell(i + 2, 1).string(item.company_name);
worksheet.cell(i + 2, 2).string(item.contact_name);
worksheet.cell(i + 2, 3).string(item.age);
worksheet.cell(i + 2, 4).string(item.email);
worksheet.cell(i + 2, 5).string(item.phone);
worksheet.cell(i + 2, 6).string(item.dsr_ratio);
worksheet.cell(i + 2, 7).string(item.max_eligibility);
worksheet.cell(i + 2, 8).string(item.created_at_local);
worksheet.cell(i + 2, 9).string(item.selected_banks);
});
return workbook.writeToBuffer().then(buffer => {
const params = {
Bucket: contactsBucket,
Key: `contacts/${id}.xlsx`,
Body: buffer,
ACL: 'public-read'
}
const fileURL = `https://${contactsBucket}.s3-${bucketRegion}.amazonaws.com/contacts/${id}.xlsx`;
return S3.upload(params).promise().then(_data => {
callback(null, response(200, fileURL));
});
})
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
The code above is the improved version from the original code I had earlier. Thank you for reading.
推荐答案
阅读 查看全文