是否可以直接访问Firebase存储资源URL?

时间:2017-05-29 16:25:08

标签: firebase firebase-storage

我正在构建一个跨平台的移动应用程序,它使用Firebase进行数据存储和用户身份验证。我也使用SDK,它需要直接URL到某些mp4文件。到目前为止,我们已将这些视频文件托管在单独的服务器上,但由于Firebase的简单性和可扩展性,我希望将它们存储在它上面而不是单独存储。但是,我需要能够拥有该文件的直接URL。

当我使用文件的Firebase存储位置从浏览器访问测试文件时,例如:

APPNAME.appspot.com/RESOURCENAME.mp4

我收到404错误。

我已将规则设定为以下内容:

service firebase.storage {
    match /b/{bucket}/o {
        match /{allPaths=**} {
            allow write: if request.auth != null;
            allow read: if true;
        }
    }
}

有没有办法直接从网址访问Firebase存储资源?

编辑:我应该补充一点,我需要一个非随机生成的URL,而是基于文件在存储中的逻辑位置,因为资源文件是根据应用程序中文件的UUID动态访问的。还有太多文件要存储/查找不可用的链接。

4 个答案:

答案 0 :(得分:1)

对云存储中文件的默认访问权限取决于您的安全规则。这是故意的。

但是,无论何时通过Firebase SDK上传文件,它都会为该文件生成所谓的下载URL。下载URL是一个不可提供的URL,它提供对文件的只读访问权限。它是按照您在此处描述的方式与应用程序的其他用户共享。

要生成下载网址,请按照此处文档中的说明操作:https://firebase.google.com/docs/storage/ios/download-files#generate_a_download_url(其他平台的文档中也存在类似的部分)。

答案 1 :(得分:1)

虽然这不是您问题的答案,但您应该真正使用Firebase Hosting来实现此目的。与基于静态内容相比,Firebase存储更倾向于用户上传和共享。

使用Firebase CLI上传到托管非常简单。 https://www.youtube.com/watch?v=meofoNuK3vo您只需将要托管的文件放在“公开”文件夹中,然后运行firebase deploy

答案 2 :(得分:1)

意识到这篇文章现在有点麻烦了,但这是我的工作功能(为Vue.js 2编写),它使用下载和显示图像所需的正确URL更新了Firestore文档('profilePic')中的字段在网页上。未经音频文件测试。

submitImage() {
      const user = firebase.auth().currentUser;
      var storageRef = storage
        .ref()
        .child(`user/${user.uid}`)
        .child(this.file.name);
      //update the storage area with the image, and add the correct download URL to the database
      storageRef
        .put(this.file)
        .then(function() {
          console.log(`Successfully uploaded profile picture!`);
          storageRef.getDownloadURL().then(function(url) {
            profiles
              .doc(user.uid)
              .update({ profilePic: url })
              .then(function() {
                console.log("Successfully updated path");
              })
              .catch(function(error) {
                console.error("There was a problem with the update", error);
              });
          });

          this.file = null;
        })
        .catch(function(error) {
          console.log("There was an error uploading the picture - ", error);
        });
    }

文件是从存储在变量“ file”中的html文件输入字段获得的,创建了一个存储路径(如果尚不存在),该存储路径以当前用户的UID命名,然后相应的firestore doc字段将使用getDownloadURL()的返回值进行更新(这是一个很长的字符串,其中包含对存储桶和访问令牌的引用)。现在,我的网络应用正确显示了图像:)。花了我一些时间来得到这个,所以我想分享对我有用的解决方案!

以供参考:https://firebase.google.com/docs/storage/web/download-files

答案 3 :(得分:0)

我试图找出与您相同的问题。因此,我检查了Firebase存储并单击图像。在那里,您应该看到一个小版本的图像。检查了Firebase的存储位置,并找到了它所在的URL。就像这样:

https://firebasestorage.googleapis.com/v0/b/" + yourappname + ".appspot.com/o/images%2F" +  imagename+ "?alt=media

我还将读写设置更改为:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read;
      allow write: if request.auth != null;
    }
  }
}
相关问题