【发布时间】:2020-05-02 02:53:05
【问题描述】:
当尝试使用 node.js 函数访问我的 Firebase 存储主目录中的图像时,我收到 [object Object] 作为响应。我想我错误地初始化了存储桶,但不确定我哪里出错了。
这是 firebase 函数中的调试信息:
ChildProcessError: `composite -compose Dst_Out [object Object] [object Object] /tmp/output_final2.png` failed with code 1
这是我的代码:
const admin = require('firebase-admin');
admin.initializeApp();
const storage = admin.storage();
const os = require('os');
const path = require('path');
const spawn = require('child-process-promise').spawn;
exports.onFileChange= functions.storage.object().onFinalize(async object => {
const bucket = storage.bucket('myID.appspot.com/');
const contentType = object.contentType;
const filePath = object.name;
console.log('File change detected, function execution started');
if (object.resourceState === 'not_exists') {
console.log('We deleted a file, exit...');
return;
}
if (path.basename(filePath).startsWith('changed-')) {
console.log('We already changed that file!');
return;
}
const destBucket = bucket;
const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
const border = bucket.file("border.png");
const mask1 = bucket.file("mask1.png");
const metadata = { contentType: contentType };
return destBucket.file(filePath).download({
destination: tmpFilePath
}).then(() => {
return spawn('composite', ['-compose', 'Dst_Out', mask1, border, tmpFilePath]);
}).then(() => {
return destBucket.upload(tmpFilePath, {
destination: 'changed-' + path.basename(filePath),
metadata: metadata
})
}); });```
【问题讨论】:
-
使用
const bucket = storage.bucket('myID.appspot.com/');您是否声明了默认存储桶? -
是的,用 const bucket = storage.bucket() 声明;不能解决问题
-
@RenaudTarnec 我猜用 const border = bucket.file("border.png"); 访问文件有问题?
-
您可以添加您存储桶的图片,显示
border.png文件吗? -
@RenaudTarnec i.stack.imgur.com/1a6DQ.jpg
标签: javascript firebase google-cloud-platform google-cloud-functions firebase-storage