资源上的“storage.objects.get”权限被拒绝

问题描述 投票:0回答:1

我正在尝试创建一个简单的验证码系统。用作数据的图片存储在 firebase 存储中。问题是,它不允许我查看它们,并且它们没有按预期显示。这就是我调用 getcaptcha 时图片的显示方式:

当我检查页面时,我发现图像网址已被检索:

<div id="captcha-container">
   <p>Select images containing birds</p>
   <img src="https://storage.googleapis.com/captcha-7f66e.appspot.com/images/stairs/stairs06.jpg" class="captcha-image"><img src="https://storage.googleapis.com/captcha-7f66e.appspot.com/images/birds/bird3.jpg" class="captcha-image"><img src="https://storage.googleapis.com/captcha-7f66e.appspot.com/images/cars/car4.jpg" class="captcha-image">
</div>

但是当我尝试通过访问网址查看图像时,它向我显示此错误:

<Error> <Code>AccessDenied</Code> <Message>Access denied.</Message> <Details>Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object. Permission 'storage.objects.get' denied on resource (or it may not exist).</Details> </Error>

我在网上查看,发现我必须添加“存储对象查看器”角色,我尝试了但仍然不起作用:

不确定这是否是添加“存储对象查看器”角色的正确位置。

我也尝试修改规则,这里是默认规则:

rules_version = '2';

// Craft rules based on data in your Firestore database
// allow write: if firestore.get(
//    /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }
  }
}

这是我定义的新规则:

rules_version = '2';

// Craft rules based on data in your Firestore database
// allow write: if firestore.get(
//    /databases/(default)/documents/users/$(request.auth.uid)).data.isAdmin;
service firebase.storage {
  match /b/{bucket}/o {
    allow read: if request.auth.uid != null;
  }
}
firebase google-cloud-platform google-cloud-storage firebase-storage google-cloud-console
1个回答
0
投票

我找到了一个解决方案来避免权限或添加角色对象查看器,当我尝试从 firebase 检索图像时,我只需使用带有过期延迟的签名 url

expiration = datetime.utcnow() + timedelta(hours=1)

def fetch_images_from_category(category, num_images):
    bucket = storage.bucket()
    blobs = bucket.list_blobs(prefix=f'images/{category}/')
    images = []
    for blob in blobs:
        if not blob.name.endswith('/'): 
            signed_url = blob.generate_signed_url(expiration)
            images.append(signed_url)
    return random.sample(images, min(num_images, len(images)))
© www.soinside.com 2019 - 2024. All rights reserved.