google-cloud-platform - 将文件从 GCP 存储桶发送到第 3 方 pdf 转换器
问题描述
我正在尝试调整 Qwiklabs 教程以使用 pdfCrowd,而不是 LibreOffice。
该服务的工作原理是从一个 GCP 存储桶中下载文件以进行“上传”,对其进行处理,然后将其上传到另一个桶以用于“已处理”文件,然后从“上传”桶中删除原始文件。
这是下载、上传、发送处理、然后删除的功能。这是来自 Qwiklabs tut 的代码,效果很好。
app.post('/', async (req, res) => {
try {
const file = decodeBase64Json(req.body.message.data);
await downloadFile(file.bucket, file.name);
const pdfFileName = await convertFile(file.name);
await uploadFile(process.env.PDF_BUCKET, pdfFileName);
await deleteFile(file.bucket, file.name);
}
catch (ex) {
console.log(`Error: ${ex}`);
}
res.set('Content-Type', 'text/plain');
res.send('\n\nOK\n\n');
})
原来的 convertFile 函数是:
async function convertFile(fileName) {
const cmd = 'libreoffice --headless --convert-to pdf --outdir /tmp ' +
`"/tmp/${fileName}"`;
console.log(cmd);
const { stdout, stderr } = await exec(cmd);
if (stderr) {
throw stderr;
}
console.log(stdout);
pdfFileName = fileName.replace(/\.\w+$/, '.pdf');
return pdfFileName;
}
当我更改我的 convertFile 函数时,问题就来了。LibreOffice 接受 file.name,但 pdfCrowd 需要文件路径。
所以我将 pdfCrowd 的函数更改为:
async function convertFile(fileName) {
// create the API client instance
const _newPdfPath = `/tmp/${fileName.replace(/\.\w+$/, '.pdf')}`
const client = new pdfcrowd.HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");
// run the conversion and write the result to a file
client.convertFileToFile(`/tmp/${fileName}`, _newPdfPath, function (err, fileName) {
if (err)
return console.error("Pdfcrowd Error: " + err);
console.log("Success: the file was created " + fileName);
});
pdfFileName = fileName.replace(/\.\w+$/, '.pdf');
return pdfFileName;
}
现在 pdf 转换返回 SUCCESS,但是在通知说没有在我传递给 convertFileToFile 的“out”文件路径中指定的文件或目录之后。_newPdfPath 指定的文件不存在。
Error: Error: ENOENT: no such file or directory, stat '/tmp/mynew.pdf'
Success: the file was created /tmp/hello (31).pdf
pdfCrowd 函数应该是在 tmp 目录中创建文件,但是异步等待在 tmp 目录中创建文件吗?
我的完整代码是:
const {promisify} = require('util');
const {Storage} = require('@google-cloud/storage');
const exec = promisify(require('child_process').exec);
const storage = new Storage();
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const pdfcrowd = require("pdfcrowd");
app.use(bodyParser.json());
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Listening on port', port);
});
app.post('/', async (req, res) => {
try {
const file = decodeBase64Json(req.body.message.data);
// console.log("FILE=========", file, req.body.message.data)
await downloadFile(file.bucket, file.name);
const pdfFileName = await convertFile(file.name);
await uploadFile(process.env.PDF_BUCKET, pdfFileName);
await deleteFile(file.bucket, file.name);
}
catch (ex) {
console.log(`Error: ${ex}`);
}
res.set('Content-Type', 'text/plain');
res.send('\n\nOK\n\n');
})
function decodeBase64Json(data) {
return JSON.parse(Buffer.from(data, 'base64').toString());
}
async function downloadFile(bucketName, fileName) {
const options = {destination: `/tmp/${fileName}`};
await storage.bucket(bucketName).file(fileName).download(options);
}
async function convertFile(fileName) {
// create the API client instance
const _newPdfPath = `/tmp/${fileName.replace(/\.\w+$/, '.pdf')}`
const client = new pdfcrowd.HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");
// run the conversion and write the result to a file
client.convertFileToFile(`/tmp/${fileName}`, _newPdfPath, function (err, fileName) {
if (err)
return console.error("Pdfcrowd Error: " + err);
console.log("Success: the file was created " + fileName);
});
pdfFileName = fileName.replace(/\.\w+$/, '.pdf');
return pdfFileName;
}
async function deleteFile(bucketName, fileName) {
await storage.bucket(bucketName).file(fileName).delete();
}
async function uploadFile(bucketName, fileName) {
await storage.bucket(bucketName).upload(`/tmp/${fileName}`);
}
解决方案
问题是您的 convertFile 函数在调用 convertFileToFile 回调之前完成。
我会将成功和错误的回调传递给 convertFile,例如
app.post('/', async (req, res) => {
try {
const file = decodeBase64Json(req.body.message.data);
// console.log("FILE=========", file, req.body.message.data)
await downloadFile(file.bucket, file.name);
let on_pdf_done = async function(pdfFileName) {
await uploadFile(process.env.PDF_BUCKET, pdfFileName);
await deleteFile(file.bucket, file.name);
res.set('Content-Type', 'text/plain');
res.send('\n\nOK\n\n');
};
let on_pdf_fail = function() {
res.set('Content-Type', 'text/plain');
res.send('\n\nERROR\n\n');
};
convertFile(file.name, on_pdf_done, on_pdf_fail);
}
catch (ex) {
console.log(`Error: ${ex}`);
}
})
function convertFile(fileName, success_callback, fail_callback) {
// create the API client instance
const _newPdfPath = `/tmp/${fileName.replace(/\.\w+$/, '.pdf')}`
const client = new pdfcrowd.HtmlToPdfClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d");
// run the conversion and write the result to a file
client.convertFileToFile(`/tmp/${fileName}`, _newPdfPath, function (err, fileName) {
if (err)
return fail_callback();
success_callback(fileName.replace(/\.\w+$/, '.pdf'));
});
}
推荐阅读
- javascript - 如何将二进制图像从 json 响应保存到 S3 存储桶
- reactjs - React路由器:构建链接时如何处理match.url中的尾随/?
- c++ - 如何避免使用 C++ 宏重新声明变量
- apache - 在 Kubernetes 中运行简单的 hello world 静态 http 应用程序
- angular - ngx-charts 图例位置错误
- outlook - 自动加载 Office 加载项 Outlook
- python - 使用未知数量的按钮
- r - 在 rmarkdown 中为图形/绘图添加标题
- python - 计算一个函数在为一个类的实例执行另一个函数后执行了多少次
- javascript - 选择下拉项目时,Vue v-autocomplete 不删除用户输入的文本