使用Firebase Cloud Functions访问Firebase存储中文件的问题,文件返回“ [object Object]”

问题描述 投票:0回答:1

[当尝试使用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('resized-')) {
        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
        })
    }); });```


javascript firebase google-cloud-platform google-cloud-functions firebase-storage
1个回答
0
投票

如果,使用

const bucket = storage.bucket('myID.appspot.com/');

您的目标是初始化default存储桶,您应该这样做

const bucket = storage.bucket();

因为您已将storage声明为admin.storage()

© www.soinside.com 2019 - 2024. All rights reserved.