Firemag Cloud功能Spawn Child Process

时间:2019-04-12 04:16:56

标签: firebase google-cloud-functions child-process imagemagick-convert

我正在尝试使用云功能将图像上传到Firebase存储之后,将图像以12:19的比例进行裁剪。

ImageMagic的'convert'命令在本地工作。但是,云功能的“生成”过程以未知/无法描述的错误退出。

firebase sample启发的异步/等待功能:

const functions = require('firebase-functions');
const cors = require('cors')({ origin: true });
const path = require('path');
const os = require('os');
const spawn = require('child-process-promise').spawn;
const fs = require('fs');

const admin = require('firebase-admin');
admin.initializeApp();

/* ... Some HTTPs Functions ... */

exports.cropWorkerPhoto = functions.storage.object().onFinalize(async (object) => {
  let filePath = object.name;
  let fileName = path.basename(filePath);

  if ( fileName.startsWith('worker_')) {

    let contentType = object.contentType;
    if (!contentType.startsWith('image/')) {
      return console.log('This is not an image.');
    }

    const bucket = admin.storage().bucket(object.bucket);
    const tempFilePath = path.join(os.tmpdir(), fileName);
    await bucket.file(filePath).download( {destination: tempFilePath} );
    console.log(`downloading ${filePath}`);
    console.log(`and storing it in ${tempFilePath}`);

    const workerID = fileName.split('_').pop();
    console.log('Cropping Picture for worker:', workerID);
    await spawn('convert', [tempFilePath, '-gravity', 'center', '-crop', '12:19!', tempFilePath]);

    const croppedFileName = `/${workerID}`;
    const croppedFilePath = path.join(path.dirname(filePath), croppedFileName);

    await bucket.upload(tempFilePath, {
      destination: croppedFilePath,
      metadata: { contentType: contentType },
    });
    console.log('Cropped Picture uploaded to: ', croppedFilePath);
    return fs.unlinkSync(tempFilePath);
  }
  return;
});

日志如下:

2019-04-12T04:05:56.135523524Z D cropWorkerPhoto: Function execution started
2019-04-12T04:05:56.135560721Z D cropWorkerPhoto: Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
2019-04-12T04:05:57.300Z I cropWorkerPhoto: downloading workers/worker_QG5obDng8Q1v6STI0wJJ
2019-04-12T04:05:57.301Z I cropWorkerPhoto: and storing it in /tmp/worker_QG5obDng8Q1v6STI0wJJ
2019-04-12T04:05:57.301Z I cropWorkerPhoto: Cropping Picture for worker: QG5obDng8Q1v6STI0wJJ
2019-04-12T04:05:59.647Z E cropWorkerPhoto: ChildProcessError: `convert /tmp/worker_QG5obDng8Q1v6STI0wJJ -gravity center -crop 12:19! /tmp/worker_QG5obDng8Q1v6STI0wJJ` failed with code 1
    at ChildProcess.<anonymous> (/srv/node_modules/child-process-promise/lib/index.js:132:23)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:915:16)
    at Socket.stream.socket.on (internal/child_process.js:336:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:561:12)
2019-04-12T04:05:59.741519767Z D cropWorkerPhoto: Function execution took 3607 ms, finished with status: 'error'

firebase sample中创建缩略图功能效果很好。

当我尝试使用“范围”而不是“裁剪”时,错误完全相同。

如何获取'convert'命令的错误输出?

如何查看Firebase服务器中安装的ImageMagick版本?

0 个答案:

没有答案