Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 将文件从GCP bucket发送到第三方pdf转换器_Google Cloud Platform_Microservices_Pdf Conversion - Fatal编程技术网

Google cloud platform 将文件从GCP bucket发送到第三方pdf转换器

Google cloud platform 将文件从GCP bucket发送到第三方pdf转换器,google-cloud-platform,microservices,pdf-conversion,Google Cloud Platform,Microservices,Pdf Conversion,我正在尝试修改Qwiklabs教程以使用pdfCrowd,而不是LibreOffice 该服务的工作原理是从一个GCP存储桶中下载一个文件进行“上传”,对其进行处理,然后将其上传到另一个存储桶中进行“处理”,然后从“上传”存储桶中删除原始文件 这是一个下载、上传、发送以供处理,然后删除的功能。这是来自Qwiklabs tut的代码,它工作得很好 app.post('/', async (req, res) => { try { const file = decodeBase64

我正在尝试修改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转换返回成功,但在收到一个通知后,该通知表示在我传递给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'));
    });
}


问题是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'));
    });
}