使用Google Cloud函数调用imagemagick'identify'

时间:2019-05-07 17:56:40

标签: imagemagick google-cloud-functions

我正在尝试在我的Google Cloud函数中使用imagemagick。通过将文件上传到Google Cloud Storage存储桶来触发该功能。我有更宏大的计划,但尝试一次达到目标。从识别开始。

    // imagemagick_setup
    const gm = require('gm').subClass({imageMagick: true});
    const path = require('path');
    const {Storage} = require('@google-cloud/storage');
    const storage = new Storage();

    exports.processListingImage = (event, context) => {
        const object = event.data || event; // Node 6: event.data === Node 8+: event
        const filename = object.name;
        console.log("Filename: ", filename);

        const fullFileObject = storage.bucket(object.bucket).file(object.name);

        console.log("Calling resize function");
        let resizePromise = resizeImage( fullFileObject );

        <more stuff>
    };


    function resizeImage( file, sizes ) {
        const tempLocalPath = `/tmp/${path.parse(file.name).base}`;

        return file
            .download({destination: tempLocalPath})
            .catch(err => {
                console.error('Failed to download file.', err);
                return Promise.reject(err);
            })
            .then( () => {
                // file now downloaded, get it's metadata
                return new Promise((resolve, reject) => {
                    gm( tempLocalPath )
                        .identify( (err, result) => {
                            if (err)
                            {
                                console.log("Error reading metadata: ", err);
                            }
                            else
                            {
                                console.log("Well, there seems to be metadata: ", result);
                            }
                        });
                });
          });
    } // end resizeImage()

本地文件路径为:“ / tmp / andy-test.raw”。但是当identity函数运行时,出现错误:

identify-im6.q16: unable to open image `/tmp/magick-12MgKrSna0qp9U.ppm': No such file or directory @ error/blob.c/OpenBlob/2701.

为什么确定要查找的文件与我(告诉我)要查找的文件不同?最终,我将调整图像的大小并将其写回到Cloud Storage,但我想获得标识才能首先运行。.

1 个答案:

答案 0 :(得分:0)

马克的答案正确-如果我上传jpg文件,则可以正常运行。面对下一个挑战。