【问题标题】:Firebase Storage - Uploading from Cloud Function results in non-accessible fileFirebase 存储 - 从 Cloud Function 上传会导致文件无法访问
【发布时间】:2020-10-18 09:07:01
【问题描述】:

大家。我已经经历了几个小时,我需要一些帮助。

我正在尝试设置一个云函数来调整上传到 Firebase 云存储的图像的大小。我关注了谷歌的文档,我通过 GCF 下载和重新上传的每个文件都无法访问,我不知道为什么。对于初学者,我只是想在函数中重新上传我正在下载的图像,但我什至无法做到这一点。

这是 GCF 本身:

const functions = require('firebase-functions');

const path = require('path');
const os = require('os');

const fs = require('fs-extra');

const { Storage } = require('@google-cloud/storage');
const gcs = new Storage();

exports.generateOptions = functions.storage.object().onFinalize(async object => {

  const bucket = gcs.bucket(object.bucket);
  const filePath = object.name;
  const fileName = filePath.split('/').pop();
  const bucketDir = path.dirname(filePath);

  const workingDir = path.join(os.tmpdir(), 'copies');
  const tmpFilePath = path.join(workingDir, fileName);

  if (fileName.includes('copy_') || !object.contentType.startsWith('image/')) {
    return false;
  }

  await fs.ensureDir(workingDir);

  await bucket.file(filePath).download({
    destination: tmpFilePath
  });

  const copyName = `copy_${fileName}`;

  await bucket.upload(tmpFilePath, {
    destination: path.join(bucketDir, copyName)
  })

  // Cleanup
  return fs.remove(workingDir);
}); 

现在,我设置了标志以查看存储桶和文件是否存在(使用来自 @google-cloud/storage 包的 bucket.exists() 和 bucket.file.exists() 方法)并且它们存在并且文件据我所知已正确下载。

我重新上传文件后,函数运行正常,我最终得到了原始文件和复制文件,如下所示:

到目前为止一切顺利。但是当我尝试打开文件时,它卡住了,没有打开它,也没有返回下载链接。

所以我尝试以另一种方式打开文件,使用这个:

点击“打开”(西班牙语为“Abrir”)后,它会打开一个新选项卡,显示以下链接:

因此,我没有获得常规的下载 URL,而是得到了“未定义”,我不知道为什么。

我希望有人有某种解决方案或解决方法,或者以前遇到过类似的问题。

这是 package.json(不管它值多少钱):

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "10"
  },
  "dependencies": {
    "@google-cloud/storage": "^5.1.1",
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1",
    "fs-extra": "^9.0.1",
    "sharp": "^0.25.4"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

任何帮助将不胜感激!

【问题讨论】:

    标签: node.js firebase google-cloud-functions google-cloud-storage firebase-storage


    【解决方案1】:

    问题是由于您通过后端代码上传文件。使用 Firebase 客户端 SDK 从网络和移动客户端上传的文件不会发生这种情况。您应该能够使用代码很好地下载文件,并且如果需要,您仍然应该能够在客户端代码中生成下载 URL。

    这其实是一个众所周知的问题,我建议你contact Firebase support directly提出问题来添加你的声音。

    【讨论】:

    • 是的,我已经从客户端顺利使用了 Firebase 存储,但鉴于 Firebase 团队自己的文档,我的印象是这也是可能的。不过感谢您的意见,我会尝试联系 Firebase 团队,看看他们能做什么。
    【解决方案2】:

    如果有人对此感到疑惑,我联系了 Firebase 支持,并被告知问题是从 Admin SDK 上传时缺少元数据。

    我得到了以下解决方法:

    await bucket.file(filePath).download({
      destination: tmpFilePath,
      metadata: {
        metadata :{
          firebaseStorageDownloadTokens: uuidv4()
        }
      },
    });
    

    当然,您需要为 node.js 安装 uuid 包(npm install uuid)。添加后,Firebase 控制台 UI 的行为正常。

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 2022-09-28
      • 2018-10-01
      • 2017-02-27
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多