【问题标题】:Is there a way to overwrite a saved file in firebase storage?有没有办法覆盖firebase存储中保存的文件?
【发布时间】:2017-05-16 09:17:27
【问题描述】:

我正在处理我的 ionic 1 应用程序中的个人资料图片。我正在寻找一种方法来更新 Firebase 存储中的文件,以使下载链接保持不变。 是否有可能或任何其他方式来实现所需的?

【问题讨论】:

    标签: ionic-framework firebase-storage


    【解决方案1】:

    您无法更新文件并维护相同的公共下载链接——它是不同的文件,因此假设您可能想要更改访问权限

    更新后重新获取URL并下载文件(实际上是在上传返回的元数据中返回,因此您可以在更改后立即将其发送到其他应用程序,无需单独获取URL) :

    var file = ... // use the Blob or File API
    ref.put(file).then(function(snapshot) {
      var url = snapshot.downloadURL;
    });
    

    【讨论】:

      【解决方案2】:

      正如另一个答案所说,基本上你不能在不更改权限的情况下更新图像。

      但是,如果您将存储图像的路径的规则更改为

      allow read: if true;
      allow write: if request.auth != null;
      

      这基本上使得任何知道链接的人都可以看到图像。这将让您从旧下载链接中看到新图像。

      但要知道,这也意味着知道此链接的任何人都可以看到图片,而不仅仅是来自您应用的人。

      【讨论】:

        【解决方案3】:

        嘿,所以我最近正在处理类似的事情。根据文档,没有直接的方法来覆盖现有文件,但我确实找到了一种方法来执行此操作并检索新的 url。这是我存储和更新的方式。

        const storeFile = async (file) => {
            const ref = firebase.firestore.collection('bin').doc();
            const id = ref.id;
            const storageRef = firebase.storage().ref().child('bin/' + id)
            await storageRef.put(file)
        }
        
        const updateFile = async (id, file) => {
            const storageRef = firebase.storage().ref('/path').child(id);
            // Put the new file in the same child ref.
            await storageRef.put(file);
            // Get the new URL
            const url = await storageRef.getDownloadURL();
            return url;
        }
        

        【讨论】:

        • 这里的id 是什么?
        • @MarkusAmaltheaMagnuson 通常我会将文件存储在唯一的 ID 下。我将在 Firestore 中创建一个 id,或者使用一个 uuid 生成器。我会更新我的答案
        【解决方案4】:

        应在https://console.cloud.google.com/storage 上进行文件更新,并在替换同名文件后,应使用 firebaseStorageDownloadTokens 键和以前使用的令牌更新其元数据。

        【讨论】:

          猜你喜欢
          • 2021-12-18
          • 2023-02-25
          • 2019-06-24
          • 2020-10-01
          • 2020-08-11
          • 2021-08-15
          • 2017-05-09
          • 2021-02-02
          • 2016-10-10
          相关资源
          最近更新 更多