尝试从Firebase存储加载图像时出现403 Forbidden错误

bbe*_*nis 7 android firebase firebase-storage

我正在使用firebase存储来为我的Android应用程序上的用户存储和加载图像.在使用之前,必须对所有用户进行身份验证.有时,某些用户个人资料图像未显示并抛出"403 Forbidden"错误.那些图像在显示之前,我不知道他们为什么停止工作.我在我的firebase存储上使用以下规则:

 service firebase.storage {
   match /b/<storage_address>.appspot.com/o {
    match /{allPaths=**} {
     allow read: if request.auth != null;
     allow write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如果我改变读取规则, allow read;所有图像都正常工作.这是我的显示方法:

Picasso.Builder builder = new Picasso.Builder(this);
                builder.listener(new Picasso.Listener()
                {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
                    {
                        exception.printStackTrace();
                    }
                });
                builder.build().load(URL).into(imgView);
Run Code Online (Sandbox Code Playgroud)

图像URL看起来像这样:

https://firebasestorage.googleapis.com/v0/b/<storage_address>.appspot.com/o/images%2Fprofile%2Fphoto%2F36805?alt=media&token=e62ec151-3aaf-4e5c-aefb-1b3c93828684
Run Code Online (Sandbox Code Playgroud)

它可能与令牌有关吗?

use*_*964 18

可能来得太晚了,但也许对其他人有帮助.我遇到了同样的问题,我的问题是firebase存储中的文件名是唯一的.如果您上传一个具有相同文件名的新文件,它将覆盖旧文件并为其生成新标记,使旧网址过时.您应该为上传的文件生成唯一的文件名,然后应该修复它.

  • 我只是将当前时间戳附加到文件名. (4认同)

Ari*_*014 13

如果您只是在测试存储,则您可能没有实施身份验证。默认情况下,Storage 要求对上传用户进行身份验证。为了绕过它,在 Storage->Rules 中写下这个:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth == null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

对我来说就像一种魅力。使用 Firebase 玩得开心

  • 注意:这实际上应该只适用于开发模式,大多数人可能会推荐一些身份验证来上传文件 (3认同)

Fel*_*aro 8

向每个人授予读取权限使我的应用程序再次正确显示图像:

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

Run Code Online (Sandbox Code Playgroud)

可能不推荐这样做,但暂时解决了问题


Chr*_*bst 5

如果文件扩展名不正确或丢失,则会出现以下错误:

 {
    "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
    }
 }
Run Code Online (Sandbox Code Playgroud)

在您提供的图像URL中,图像名称后没有该图像的扩展名。

我已经使用自己的网址之一进行了测试,可以正常工作(可以测试):https : //firebasestorage.googleapis.com/v0/b/training-a0a3e.appspot.com/o/penguin-56101_640.jpg?alt=media&token = 8dfb913d-e2f3-4956-bd3a-2c3746d0d6d3

但是,当删除.jpg时:https : //firebasestorage.googleapis.com/v0/b/training-a0a3e.appspot.com/o/penguin-56101_640? alt = media & token = 8dfb913d-e2f3-4956-bd3a-2c3746d0d6d3

收到以下响应:

 {
    "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
    }
 }
Run Code Online (Sandbox Code Playgroud)