我正在尝试在我的Google Cloud函数中使用imagemagick。上传文件到Google云存储存储桶即可触发该函数。我有更宏伟的计划,但试图一步一步地实现。以identify开头。
// 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。但是当identify函数运行时,我得到一个错误:
identify-im6.q16: unable to open image `/tmp/magick-12MgKrSna0qp9U.ppm': No such file or directory @ error/blob.c/OpenBlob/2701.为什么identify要查找的文件与我告诉它要查找的文件不同?最终,我将调整镜像的大小,并将其写回云存储,但我想让identify先运行。
发布于 2019-05-08 04:17:18
Mark给出了正确的答案--如果我上传了一个jpg文件,它就能工作。进入下一个挑战。
https://stackoverflow.com/questions/56028128
复制相似问题