【问题标题】:Error: Cloud Functions for Firebase spawn EACCES (ghostscript)错误:Firebase 的 Cloud Functions 生成 EACCES(ghostscript)
【发布时间】:2018-11-21 19:57:00
【问题描述】:

我尝试使用 Firebase Cloud Functions 创建 PDF 文件的缩略图。 调用 gs 后出现以下错误:

2018-06-12T11:29:08.685Z E makeThumbnail:错误:生成 EACCES

在exports._errnoException (util.js:1020:11)

在 ChildProcess.spawn (internal/child_process.js:328:11)

在exports.spawn (child_process.js:370:9)

在 Object.exec (/user_code/node_modules/gs/index.js:86:28)

在 Promise (/user_code/index.js:95:12)

在 mkdirp.then.then (/user_code/index.js:86:12)

2018-06-12T11:29:08.698166767Z D makeThumbnail:函数执行耗时 780 毫秒,完成状态为:“错误”

是否需要使用ghostscript之类的组件才能使用Spark以外的计划?

另外,我的代码。也许我只是没有在代码中看到我的问题

const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const admin = require('firebase-admin');

const spawn = require('child-process-promise').spawn;
const path = require('path');
const os = require('os');
const fs = require('fs');
const gs = require('gs');
const THUMB_MAX_HEIGHT = 200;
const THUMB_MAX_WIDTH = 200;
const THUMB_PREFIX = 'thumb_';
const gs_exec_path = path.join(__dirname, './lambda-ghostscript/bin/gs');

try{admin.initializeApp(functions.config().firebase); } catch(e) {}

exports.makeThumbnail = functions.storage.object().onFinalize((object) => {

  const filePath = object.name;
  const contentType = object.contentType; 
  const fileDir = path.dirname(filePath);
  const fileName = path.basename(filePath);
  const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX} ${fileName}`));
  const tempLocalFile = path.join(os.tmpdir(), filePath);
  const tempLocalDir = path.dirname(tempLocalFile);
  const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

  const tmp_dir = os.tmpdir();


  if (fileName.startsWith(THUMB_PREFIX)) {
    console.log('Is thumbnail');
    return null;
  }


  const bucket = gcs.bucket(object.bucket);
  const file = bucket.file(filePath);
  const thumbFile = bucket.file(thumbFilePath);
  const metadata = {
    contentType: contentType,
  };

  return mkdirp(tmp_dir).then(() => {
     console.log("Dir Created");

    console.log(tempLocalFile);
    return file.download({destination: tempLocalFile});
  }).then(() => {
    console.log("File downloaded");
    if(!contentType.startsWith("image/")){

      return new Promise((resolve, reject) => {
      const pg= 1;
      gs().batch().nopause()
        .option(`-dFirstPage=${pg}`)
        .option(`-dLastPage=${pg}`)
        .executablePath(gs_exec_path)
        .device('png16m')
        .output(tempLocalThumbFile+".png")
        .input(tempLocalFile)
        .exec(err => err ? reject(err) : resolve());
      });
    }
    else
    {
      var args = [ tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile ];
      return spawn('convert', args, {capture: ['stdout', 'stderr']});
    }
  }).then(() => {

    return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath });
  }).then(() => {

    fs.unlinkSync(tempLocalFile);
    fs.unlinkSync(tempLocalThumbFile);

    return result[0];
  });

});

【问题讨论】:

    标签: javascript google-cloud-functions ghostscript


    【解决方案1】:

    经过几个小时的摸索和一遍又一遍地毫无意义地运行相同的代码,我终于找到了问题!

    您定义的可执行路径不正确。应该是'gs'

    这是一个完整的 gs() 调用示例:

    gs()
        .batch()
        .option('-dFirstPage=2')
        .option('-dLastPage=2')
        .nopause()
        .res(90)
        .executablePath('gs')
        .device('jpeg')
        .output(tempNewPath2)
        .input(tempFilePath)
        .exec((err, stdout, stderr) => {
          if (!err) {
            console.log('gs executed w/o error');
            console.log('stdout', stdout);
            console.log('stderr', stderr);
            resolve();
          } else {
            console.log('gs error:', err);
            reject(err);
          }
        });
    

    如需更多帮助,您可以查看我为此问题创建的示例代码库 https://github.com/krharsh17/ghostscript-firebase-sample

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2019-10-02
      • 1970-01-01
      • 2018-08-21
      • 2018-06-17
      • 2019-02-04
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多