NodeJSプロジェクトをAPI Gatewayに移行する途中で、Lambdaからファイルをダウンロードする方法がわかりません。API Gateway Response Node、PDFMake、Serverlessを使用してファイルをダウンロードする
ローカルノードプロジェクトのレスポンスコードのスニペットです。
app.get('/downloadPDF', function (req, res) {
res.setHeader('Content-disposition', 'attachment; filename=test.pdf');
res.setHeader('Content-type', 'application/pdf');
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(res);
pdfDoc.end();
});
応答にパイプするPDFを返すことができました。
ここでは、サーバーレスの私のラムダ機能の抜粋です。ここで
module.exports.createPDF = (event, context) => {
var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);
var pdfDoc = printer.createPdfKitDocument(dd);
pdfDoc.pipe(res);
pdfDoc.end();
}
は、私はパイプにラムダでレスポンスオブジェクトへの参照を取得する方法がわからない私のserverless.yml
createPDF:
handler: functions.myFunction
events:
- http:
path: services/getPDF
method: get
response:
headers:
Content-Type: "'application/pdf'"
Content-disposition: "'attachment; filename=test.pdf'"
でエンドポイントです。それは可能ですか?別の方法がありますか?
更新
私は、クライアント上のJSONレスポンスとデコードにPDFバイナリbase64エンコードを返すことによって、この問題を解決することになりました。 注:レスポンスマッピングテンプレートでbase64のデコードを使用しても機能しませんでした。
サンプルコード:
var buffers = [];
pdfDoc.on('data', buffers.push.bind(buffers));
pdfDoc.on('end', function() {
var bufCat = Buffer.concat(buffers);
var pdfBase64 = bufCat.toString('base64');
return cb(null,
{"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": pdfBase64});
});
ありがとうございました!残念ながら私はこのルートには成功しませんでしたが、レスポンスマッピングテンプレートの経験があります。 – Padawan