【问题标题】:Problem with accessing file in Firebase Storage with Firebase Cloud Functions, file returns "[object Object] "使用 Firebase Cloud Functions 访问 Firebase Storage 中的文件时出现问题,文件返回“[object Object]”
【发布时间】: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 文件吗?

标签: javascript firebase google-cloud-platform google-cloud-functions firebase-storage


【解决方案1】:

如果,用

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

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

const bucket = storage.bucket();

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


更新(在您对 const border = bucket.file("border.png"); 发表评论之后)

此外,通过查看similar Cloud Function 的代码(来自官方示例,使用ImageMagickspawn),您似乎不应该将一些File 对象传递给spawn() 方法创建通过 Cloud Storage Node.js 客户端 API 的file() 方法(即const border = bucket.file("border.png");),但您之前保存了一些文件到临时目录。

请看上面提到的云函数示例的以下摘录。他们定义了一些临时目录和文件路径(使用path模块),将文件下载到这个目录并使用它们来调用spawn()方法。

  //....
  const filePath = object.name;
  const contentType = object.contentType; // This is the image MIME type
  const fileDir = path.dirname(filePath);
  const fileName = path.basename(filePath);
  const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));   // <---------
  const tempLocalFile = path.join(os.tmpdir(), filePath);    // <---------
  const tempLocalDir = path.dirname(tempLocalFile);     // <---------
  const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);     // <---------

  //....

  // Cloud Storage files.
  const bucket = admin.storage().bucket(object.bucket);
  const file = bucket.file(filePath);
  const thumbFile = bucket.file(thumbFilePath);
  const metadata = {
    contentType: contentType,
    // To enable Client-side caching you can set the Cache-Control headers here. Uncomment below.
    // 'Cache-Control': 'public,max-age=3600',
  };

  // Create the temp directory where the storage file will be downloaded.
  await mkdirp(tempLocalDir)   // <---------
  // Download file from bucket.
  await file.download({destination: tempLocalFile});    // <---------
  console.log('The file has been downloaded to', tempLocalFile);
  // Generate a thumbnail using ImageMagick.
  await spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile], {capture: ['stdout', 'stderr']});   
  //.....

【讨论】:

  • 谢谢,试过了,但我仍然得到 [object Object] [object Object]
  • 非常感谢,这解决了使用存储文件的问题。此外,我发现“复合”不能与 firebase 功能一起使用;显然它只适用于“转换”。这里仍有一些问题,但与主要问题无关。
【解决方案2】:

您无法将 Cloud Storage 文件类型的对象传递给 spawn。您需要传递将用于创建命令行的字符串。这意味着您需要先将这些文件下载到本地/tmp,然后才能使用它们 - ImageMagick 不知道如何处理 Cloud Storage 中的文件。

【讨论】:

    猜你喜欢
    • 2017-11-03
    • 2017-08-27
    • 1970-01-01
    • 2019-01-16
    • 2018-12-13
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    相关资源
    最近更新 更多