Firebase函数从存储中获取文件

时间:2018-09-01 19:47:52

标签: firebase google-cloud-functions

我必须将文件发送到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[[]]

1 个答案:

答案 0 :(得分:0)

您需要将文件从存储桶下载到本地函数上下文env。 Firebase函数开始执行后,您可以调用以下命令: 下面的内容或多或少应该可以工作,只需调整您的需求即可。在您的.onCall上下文中调用它,您就会明白

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);
}