问题我最近切换到无服务器架构,无法访问我的文件下面的代码在本地环境中运行良好,但它不在 lambda 函数中执行,它给了我以下错误
它给了我以下错误
Error: ENOENT: no such file or directory, open 'tmp/file 1-1692962065917.pdf'
你能指导我如何访问 lambda 函数中的文件吗
这是我的代码
const serverless = require("serverless-http");
const express = require("express");
const multer = require('multer');
const fs = require('fs');
const { PDFDocument } = require('pdf-lib');
const http = require('http');
// const app = express();
const cors = require('cors');
const os = require('os');
const cluster = require('cluster');
const { default: e } = require('express');
const path = require('path');
// count total number of cpu
const cpuNums = os.cpus().length;
console.log("Total Number of Cpus(CORS):"+cpuNums);
var date = new Date();
const app = express();
var check=0;
app.use(cors({
origin: '*',
}));
var fname;
// Set up multer to handle file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './tmp/');
},
filename: (req, file, cb) => {
check++;
fname = `${file.fieldname} ${check}-${Date.now()}.pdf`;
console.log("Filename : "+fname);
cb(null, fname);
},
});
const upload = multer({ storage });
app.post('/', upload.single('file'), (req, res,next) => { console.log("=================================================");
console.log("API Last Build Date " + date.toLocaleDateString());
console.log("API Last Build Time" + date.toLocaleTimeString());
console.log(date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate());
console.log("===========================");
console.log("LAST API CALL TIME:--" + date.toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }));
console.log("===========================");
console.log("Your IP Addresss is: " + req.socket.localAddress);
console.log("=================================================");
// Read the input PDF file
const file = req.file;
if (!file) {
const error = new Error("Please upload a file");
error.httpStatusCode = 400;
return next(error);
}
// crop uploaded file
async function cropPDF(inputPath, outputPath, x, y, width, height) {
// Read the input PDF file
let pdfDoc = await PDFDocument.load(fs.readFileSync(inputPath));
// Loop through all pages in the PDF
for (let i = 0; i < pdfDoc.getPages().length; i++) {
// Get the page that you want to crop
let page = pdfDoc.getPage(i);
// Set the crop box for the page
page.setCropBox(x, y, width, height);
}
// console.log();
// Save the output PDF file
fs.writeFileSync(outputPath, await pdfDoc.save());
fs.unlink( './tmp/'+fname, (err) => {
if (err) {
console.error(err);
} else {
console.log('File deleted successfully');
}
});
res.download(outputPath);
// fs.unlink();
}
if(req.body.Ecommerce==2)
{
cropPDF('./tmp/' + fname,'outputfiledownload.pdf', 170, 467, 255, 353)
.then(() => {
console.log("PDF is cropped");
// PDF has been cropped
})
.catch((error) => {
console.log(error);
});
}
});
module.exports.handler = serverless(app);
代码总结:
我首先用户上传pdf并保存,然后根据用户选择调用函数并执行操作,然后删除文件
您需要将所有
./tmp
路径更改为 /tmp
。 ./tmp
路径转换为“当前目录下的 tmp
文件夹”,在本例中是 Lambda 函数运行的目录。在 AWS Lambda 环境中,您无法写入 Lambda 函数运行的目录中。您只能写入/tmp
目录。