Firebase函数从存储器中获取文件

Firebase函数从存储器中获取文件,firebase,google-cloud-functions,Firebase,Google Cloud Functions,我必须向API发送一个文件,因此我必须使用fs.readFileSync()。将图片上传到存储器后,我调用我的函数来执行API调用。但我无法从存储器中获取文件。这是代码的一部分,它在结果中总是为null。我还尝试在没有参数的情况下.getFiles(),然后我得到了所有文件,但我不想通过迭代过滤它们 exports.stripe_uploadIDs = functions.https //.region("europe-west1") .onCall((data, context)

我必须向API发送一个文件,因此我必须使用
fs.readFileSync()
。将图片上传到存储器后,我调用我的函数来执行API调用。但我无法从存储器中获取文件。这是代码的一部分,它在结果中总是为null。我还尝试在没有参数的情况下
.getFiles()
,然后我得到了所有文件,但我不想通过迭代过滤它们

    exports.stripe_uploadIDs = functions.https //.region("europe-west1")
  .onCall((data, context) => {
    const authID = context.auth.uid;
    console.log("request is authentificated? :" + authID);

    if (!authID) {
      throw new functions.https.HttpsError("not authorized", "not authorized");
    }

    let accountID;
    let result_fileUpload;
    let tempFile = path.join(os.tmpdir(), "id_front.jpg");

    const options_id_front_jpeg = {
      prefix: "/user/" + authID + "/id_front.jpg"
    };

    const storageRef = admin
      .storage()
      .bucket()
      .getFiles(options_id_front)
      .then(results => {
        console.log("JPG" + JSON.stringify(results));
        // need to write this file to tempFile
        return results;
      });

    const paymentRef = storageRef.then(() => {
      return admin
        .database()
        .ref("Payment/" + authID)
        .child("accountID")
        .once("value");
    });

    const setAccountID = paymentRef.then(snap => {
      accountID = snap.val();
      return accountID;
    });

    const fileUpload = setAccountID.then(() => {
      return Stripe.fileUploads.create(
        {
          purpose: "identity_document",
          file: {
            data: tempFile,  // Documentation says I should use fs.readFileSync("filepath")
            name: "id_front.jpg",
            type: "application/octet-stream"
          }
        },
        { stripe_account: accountID }
      );
    });

    const fileResult = fileUpload.then(result => {
      result_fileUpload = result;
      console.log(JSON.stringify(result_fileUpload));
      return result_fileUpload;
    });

    return fileResult;
  });
结果是:

JPG[[]]

您需要将文件从bucket下载到本地函数context env。 Firebase函数开始执行后,可以调用以下命令: 下面的内容或多或少都会起作用,只要根据您的需要调整即可。在你的
中调用它。一旦调用
上下文,你就明白了

import admin from 'firebase-admin';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';

admin.initializeApp();
const { log } = console;

async function tempFile(fileBucket: string, filePath: string) {

  const bucket = admin.storage().bucket(fileBucket);
  const fileName = 'MyFile.ext';
  const tempFilePath = path.join(os.tmpdir(), fileName);
  const metadata = {
    contentType: 'DONT_FORGET_CONTEN_TYPE'
  };

  // Donwload the file to a local temp file
  // Do whatever you need with it
  await bucket.file(filePath).download({ destination: tempFilePath });
  log('File downloaded to', tempFilePath);

  // After you done and if you need to upload the modified file back to your
  // bucket then uploaded
  // This is optional
  await bucket.upload(tempFilePath, {
    destination: filePath,
    metadata: metadata
  });

  //free up disk space by realseasing the file.
  // Otherwise you might be charged extra for keeping memory space
  return fs.unlinkSync(tempFilePath);
}

我不清楚你在这里想干什么。你的代码片段不是一个完整的云函数定义,这并没有帮助——我不知道它是什么类型的触发器。你能把你的问题编辑得更清楚些吗?我已经更新了代码听起来好像你想在你创建的文件对象上使用download()。