Javascript 错误:Firebase生成EACCES的云函数(ghostscript)
我尝试使用Firebase云函数创建PDF文件的缩略图。 调用gs后,我得到以下错误: 2018-06-12T11:29:08.685Z E生成缩略图:错误:生成EACCES 在导出时。\u errnoException(util.js:1020:11) 在ChildProcess.spawn(内部/child_process.js:328:11) 在exports.spawn(child_process.js:370:9) 在Object.exec(/user\u code/node\u modules/gs/index.js:86:28) 承诺时(/user_code/index.js:95:12) 在mkdirp.then.then(/user_code/index.js:86:12) 2018-06-12T11:29:08.698166767Z D生成缩略图:函数执行耗时780毫秒,完成状态为:“错误” 是否有必要使用像ghostscript这样的组件来使用Spark以外的计划 另外,我的代码。也许我只是在代码中看不到我的问题Javascript 错误:Firebase生成EACCES的云函数(ghostscript),javascript,google-cloud-functions,ghostscript,Javascript,Google Cloud Functions,Ghostscript,我尝试使用Firebase云函数创建PDF文件的缩略图。 调用gs后,我得到以下错误: 2018-06-12T11:29:08.685Z E生成缩略图:错误:生成EACCES 在导出时。\u errnoException(util.js:1020:11) 在ChildProcess.spawn(内部/child_process.js:328:11) 在exports.spawn(child_process.js:370:9) 在Object.exec(/user\u code/node\u mo
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];
});
});
经过几个小时的挠头,一遍又一遍毫无意义地运行相同的代码,我终于找到了问题所在 您定义的可执行路径不正确。它应该是
'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);
}
});
要获得更多帮助,您可以查看我为此问题创建的示例回购