使用 aws-nodejs 模板在无服务器中生成二维码
Generating QR codes in serverless with aws-nodejs template
我需要编写一个 lambda 函数并在 api 请求字段中发送一个数字以生成 QR 码的数量并将它们存储在 S3 中 bucket.I 我正在使用无服务器框架和aws-nodejs 模板。
为了简要描述任务,假设我在 api 请求 PathParameters 中输入了一个数字,并且基于这些数字,我必须使用 qr npm 包生成这些数量的二维码,然后将这些生成的二维码存储在 s3 存储桶中
这就是我到目前为止能够做到的。
module.exports.CreateQR = (event,context) =>{
const numberOfQR = JSON.parse(event.pathParameters.number) ;
for(let i=0;i<numberOfQR;i++){
var d= new Date();
async function createQr(){
let unique, cipher, raw, qrbase64;
unique = randomize('0', 16);
cipher = key.encrypt(unique);
raw = { 'version': '1.0', data: cipher, type: 'EC_LOAD'}
// linkArray.forEach( async (element,index) =>{
let qrcode = await qr.toDataURL(JSON.stringify(raw));
console.log(qrcode);
// fs.writeFileSync('./qr.html', `<img src="${qrcode}">`)
const params = {
Bucket:BUCKET_NAME,
Key:`QR/${d}/img${i+1}.jpg`,
Body: qrcode
};
s3.upload(params , function(err,data){
if(err){
throw err
}
console.log(`File uploaded Successfully .${data.Location}`);
});
}
createQr();
}
};
我已经能够将给定数量的图像上传到存储桶,但我面临的问题是图像没有进入 order.I 我认为问题出在异步代码上。知道如何解决这个问题
那是因为您没有等待 s3 上传,而是有一个回调。
您应该使用 s3 的 .promise
然后等待它,这样您将等待文件上传,然后再转到下一个
我更改了示例代码
查看文档:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#promise-property
// see the async keyword before the lambda function
// we need it for use the await keyword and wait for a task to complete before continue
module.exports.CreateQR = async (event,context) =>{
const numberOfQR = JSON.parse(event.pathParameters.number) ;
// moved your function out of the loop
function createQr(){
// ...
// here we call .promise and return it so we get a Task back from s3
return s3.upload(params).promise();
}
for(let i=0;i<numberOfQR;i++){
// ....
// here we await for the task, so you will get the images being created and uploaded in order
await createQr();
}
};
希望它能指导您找到解决方案。
我需要编写一个 lambda 函数并在 api 请求字段中发送一个数字以生成 QR 码的数量并将它们存储在 S3 中 bucket.I 我正在使用无服务器框架和aws-nodejs 模板。 为了简要描述任务,假设我在 api 请求 PathParameters 中输入了一个数字,并且基于这些数字,我必须使用 qr npm 包生成这些数量的二维码,然后将这些生成的二维码存储在 s3 存储桶中
这就是我到目前为止能够做到的。
module.exports.CreateQR = (event,context) =>{
const numberOfQR = JSON.parse(event.pathParameters.number) ;
for(let i=0;i<numberOfQR;i++){
var d= new Date();
async function createQr(){
let unique, cipher, raw, qrbase64;
unique = randomize('0', 16);
cipher = key.encrypt(unique);
raw = { 'version': '1.0', data: cipher, type: 'EC_LOAD'}
// linkArray.forEach( async (element,index) =>{
let qrcode = await qr.toDataURL(JSON.stringify(raw));
console.log(qrcode);
// fs.writeFileSync('./qr.html', `<img src="${qrcode}">`)
const params = {
Bucket:BUCKET_NAME,
Key:`QR/${d}/img${i+1}.jpg`,
Body: qrcode
};
s3.upload(params , function(err,data){
if(err){
throw err
}
console.log(`File uploaded Successfully .${data.Location}`);
});
}
createQr();
}
};
我已经能够将给定数量的图像上传到存储桶,但我面临的问题是图像没有进入 order.I 我认为问题出在异步代码上。知道如何解决这个问题
那是因为您没有等待 s3 上传,而是有一个回调。
您应该使用 s3 的 .promise
然后等待它,这样您将等待文件上传,然后再转到下一个
我更改了示例代码
查看文档:
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3/ManagedUpload.html#promise-property
// see the async keyword before the lambda function
// we need it for use the await keyword and wait for a task to complete before continue
module.exports.CreateQR = async (event,context) =>{
const numberOfQR = JSON.parse(event.pathParameters.number) ;
// moved your function out of the loop
function createQr(){
// ...
// here we call .promise and return it so we get a Task back from s3
return s3.upload(params).promise();
}
for(let i=0;i<numberOfQR;i++){
// ....
// here we await for the task, so you will get the images being created and uploaded in order
await createQr();
}
};
希望它能指导您找到解决方案。