如何在 Cloud Functions 存储触发器中获取经过身份验证的 Firebase 用户的 uid

The*_*Rex 1 android firebase google-cloud-functions firebase-storage google-cloud-firestore

背景:我正在使用 Firebase Cloud Functions、新的 Firestore 数据库和带有 Android 客户端的存储桶。

我想要完成的任务: 当用户将图片上传到存储桶时,我想使用云函数获取存储桶中图像位置的文件路径/链接,并将该字符串作为新文档存储在新集合下在 Firestore 中当前登录的用户文档下称为“图片”。

这样,我可以看到每个用户直接在 Firestore 中上传的图像,并且可以更轻松地将特定用户的图像拉至 Android 客户端。

到目前为止我已经完成的工作: 1. 当用户第一次登录时,它会在新的 Firestore 数据库中创建一个用户文档。2. 登录用户可以将图片上传到存储桶。3.使用Firebase Cloud Functions,我设法获取存储位置的文件路径/链接,如下所示:

/**
 * When a user selects a profile picture on their device during onboarding,
 * the image is sent to Firebase Storage from a function running on their device. 
 * The cloud function below returns the file path of the newly uploaded image. 
 */
exports.getImageStorageLocationWhenUploaded = functions.storage.object().onFinalize((object) => {
  const filePath = object.name; // File path in the bucket.
  const contentType = object.contentType; // File content type.

  // Exit if this is triggered on a file that is not an image.
  if (!contentType.startsWith('image/')) {
    console.log('This is not an image.');
    return null;
  }
console.log(filePath);
});
Run Code Online (Sandbox Code Playgroud)

问题:如何使用 Cloud Functions 获取当前登录的用户并将该用户上传的图像文件路径/链接作为新文档存储在 Firestore 数据库中该登录用户的文档下?

Dou*_*son 6

目前,使用 Cloud Storage 触发器,您无法访问经过身份验证的用户信息。要解决这个问题,您必须执行一些操作,例如将 uid 嵌入文件的路径中,或者将 uid 添加为文件上传中的元数据。