Javascript 带有ChildProcessError的Google云函数错误
这是我的云函数,它应该在每次上传图像时生成带水印的图像并将其存储在firebase存储中Javascript 带有ChildProcessError的Google云函数错误,javascript,firebase,google-cloud-functions,imagemagick,Javascript,Firebase,Google Cloud Functions,Imagemagick,这是我的云函数,它应该在每次上传图像时生成带水印的图像并将其存储在firebase存储中 exports.generateWatermark = functions.storage .object() .onFinalize(async object => { try { const fileBucket = object.bucket; // The Storage bucket that contains the file. const file
exports.generateWatermark = functions.storage
.object()
.onFinalize(async object => {
try {
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
// Exit if this is triggered on a file that is not an image.
if (!contentType.startsWith('image/')) {
return console.log('This is not an image.');
}
// Get the file name.
const fileName = path.basename(filePath);
// Exit if the image is already a watermarked image.
if (fileName.startsWith('watermark_')) {
return console.log('Already a Watermarked image.');
}
if (!filePath.startsWith('pets')) {
return console.log('Not a pet image: ', filePath);
}
// Download file from bucket.
const bucket = admin.storage().bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
const tempWatermarkPath = path.join(os.tmpdir(), 'watermark.png');
const metadata = {
contentType: contentType,
};
// Generate a watermarked image using Jimp
await bucket.file(filePath).download({destination: tempFilePath});
await bucket
.file('logo/cbs.png')
.download({destination: tempWatermarkPath});
console.log('Image downloaded locally to', tempFilePath, filePath);
await spawn('convert', [
tempFilePath,
'-gravity',
'NorthWest',
'-draw',
`"image Over 10,10,200,200 ${tempWatermarkPath}"`,
tempFilePath,
]);
console.log('Watermarked image created at', tempFilePath);
// We add a 'watermark_' prefix
const watermarkFileName = `watermark_${fileName}`;
const watermarkFilePath = path.join(
path.dirname(filePath),
watermarkFileName,
);
// Uploading the watermarked image.
await bucket.upload(tempFilePath, {
destination: watermarkFilePath,
metadata: metadata,
});
// Once the watermarked image has been uploaded delete the local file to free up disk space.
fs.unlinkSync(tempFilePath);
return fs.unlinkSync(tempWatermarkPath);
} catch (err) {
console.log('GENERATE WATERMARK ERROR: ', err);
throw err;
}
});
出错的代码部分是imagemagick部分:
await spawn('convert', [
tempFilePath,
'-gravity',
'NorthWest',
'-draw',
`"image Over 10,10,200,200 ${tempWatermarkPath}"`,
tempFilePath,
]);
这是我得到的错误:
有没有办法让我得到更多关于错误的信息?错误甚至没有到达我的捕获块。使用观察者模式
调用childprocess.spawn
的返回值与stdout和stderr一起使用,它们是eventemitter
在等待现有接口之前,您需要一个额外的步骤来提示它。比如说,
constspawn=(命令,参数)=>newpromise((解析,拒绝)=>{
const cp=require('child_process').spawn(命令,args);
设err=null,out=null;
on('data',data=>out+=data.toString());
on('error',data=>err+=data.toString());
cp.on('error',data=>err+=data.toString());
cp.on('close',代码=>{
(代码===0)?解决(输出):拒绝(错误)
});
})
另一方面,使用回调。这使得使用util.promisify
函数很容易实现。比如说
const util=require('util');
const execFile=util.promisify(require('child_process').execFile);
exports.generateWatermark=functions.storage
.object()
.onFinalize(异步对象=>{
试一试{
//...
等待execFile('convert'[
临时文件路径,
“-重力”,
"西北",,
“-绘制”,
`“超过10,10200200${tempWatermarkPath}的图像”`,
临时文件路径,
]);
//...
}捕捉(错误){
log('GENERATE WATERMARK ERROR:',err);
犯错误;
}
});