typescript - 如何使用 Firebase Firestore 在客户端上缓存 getDownloadUrl() 调用?

标签 typescript react-native google-cloud-storage firebase-storage react-native-firebase

我在后端数据库中存储了一些图像/视频的 Firebase 存储路径,移动客户端使用这些路径在我的 View 需要时获取下载 URL:

import {firebase} from '@react-native-firebase/storage';

// e.g. 'images/stars.jpg'
const getDownloadUrlForFilePath = (filePath: string | null): Promise<string> => {
  if (filePath === null) {
    throw 'Path is null!';
  }
  return getDownloadUrlForRef(firebase.storage().ref(filePath));
};

const getDownloadUrlForRef = (ref): Promise<string> => {
  return ref.getDownloadURL();
};

问题是我在很多地方都需要相同的图像。所以我经常打电话getDownloadURL()检索相同的下载网址。这会导致手机对我调试网络流量时出现的同一资源发出多个 http 请求。

在我当前的实现中,我没有将检索到的下载 URL 存储在移动客户端中的任何位置。

我想找到一种方法让应用程序只为同一路径获取一次下载 URL 并缓存它。 Firebase SDK 是否支持此功能?

最佳答案

下载 URL 从生成的那一刻起一直有效,直到您撤销它(从 Firebase 控制台)。因此,存储它并重新使用它确实是安全的。

事实上,大多数通过 Firebase 使用 Cloud Storage 的应用程序会在上传图像后立即生成下载 URL,然后将下载 URL 以及有关图像的元数据存储在数据库中。

你通常会:

  • 将下载 URL 传递给需要未经身份验证访问图像的客户端。
  • 将路径传递给需要对图像进行身份验证访问的客户端,例如因为他们可能需要更新数据。

  • 如果您只存储下载 URL(与许多应用程序一样),则在第二种情况下,您将通过调用 Storage.refFromUrl() method 将该下载 URL 映射回相应的路径。 .

    关于typescript - 如何使用 Firebase Firestore 在客户端上缓存 getDownloadUrl() 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703069/

    相关文章:

    python - 将文件从 Google Compute Engine VM 上运行的 docker 镜像复制到 Google Storage 存储桶

    php - 具有云存储的 Google App Engine 无法在本地运行

    reactjs - 使来自 react-intl 的 FormattedMessage 中的 id 继承自自定义 TypeScript 接口(interface)以启用 VS IntelliSense 和类型检查

    javascript - 在什么时候计算 scrollHeight

    TypeScript:子类型化和协变参数类型

    google-cloud-platform - 如何更改云存储中多个文件的扩展名?

    javascript - typescript 路径无法解析

    android - 任务 ':react-native-custom-tabs:transformDexArchiveWithExternalLibsDexMergerForDebugAndroidTest'的执行失败

    ios - 如何在 React Native 中检查/更改 iOS 版本?

    ios - react native : can't load image