firebase存储安全规则文件大小限制权限拒绝

发布于 2025-02-08 09:08:39 字数 1817 浏览 1 评论 0原文

我正在尝试从我的JavaScript代码(EXPO)上传斑点。我继续获得许可错误。我尝试了Firebase控制台提供的操场,但这无济于事,因为它并不能反映代码中发生的情况。

[未经手的承诺拒绝:FirebaseError:Firebase Storage:用户无权访问

以下是我的存储安全规则的简化版本,

service firebase.storage {
  match /b/{bucket}/o {
    match /{filename} {
      allow read, write: if request.resource.size < 99 * 1024 * 1024;
    }
  }
}

我上传的文件非常小(只有400个字节)。

我确保通过简单地尝试执行安全规则结构是正确的 允许阅读,写:如果是true,并且效果很好(上传了500B文本文件,没有问题)。

我不确定为什么从

下面的文档中提取的如此简单的安全规则是我用于上传的代码,我为什么会获得权限拒绝错误。我不确定我上传的方式(使用uploadbytes()或blob或xmlhttprequest)与此有关。

export async function uploadFileToFirebaseAsync(uri, uid, subfolderPath, fileExtension, filename) {

    // Why are we using XMLHttpRequest? See:
    // https://github.com/expo/expo/issues/2402#issuecomment-443726662
    const blob = await new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = function () {
            resolve(xhr.response);
        };
        xhr.onerror = function (e) {
            console.error(e);
            reject(new TypeError("Network request failed"));
        };
        xhr.responseType = "blob";
        xhr.open("GET", uri, true);
        xhr.send(null);
    });

    const fileRef = ref(getStorage(), `${filename + fileExtension}`);    
    const result = await uploadBytes(fileRef, blob);

    // We're done with the blob, close and release it
    blob.close();

    return await getDownloadURL(fileRef);
}

我以为“也许” Firebase存储无法确定斑点的尺寸,因此我还试图通过以下内容将大小作为Custommetadata传递。仍然没有不幸。

等待上载bytbytes(fileref,blob,{custommetadata:{size:blob.size}});

,然后将安全规则更改为之类的

允许读取,写入,nif request.resource.metadata。 Custommetadata.Size&lt; 99 * 1024 * 1024;

I'm trying to upload a blob from my javascript code (EXPO). I keep getting permission error. I'v tried the playground provided by the firebase console but that does not help much as it doesn't reflect what is going on in the code.

[Unhandled promise rejection: FirebaseError: Firebase Storage: User does not have permission to access

Below is a simplified version of my storage security rule

service firebase.storage {
  match /b/{bucket}/o {
    match /{filename} {
      allow read, write: if request.resource.size < 99 * 1024 * 1024;
    }
  }
}

The file that I am uploading is very small (only 400 bytes) roughly.

I made sure that the security rule structure is correct by simply trying to do
allow read, write: if true and that works fine (Upload a 500B text file with no problem).

Im not sure why I am getting permission denied error with such simple security rule extracted from the documentation

Below is the code I use to upload. I'm not sure if the way that I am uploading (using uploadBytes() or blob or XMLHttpRequest) has something to do with this.

export async function uploadFileToFirebaseAsync(uri, uid, subfolderPath, fileExtension, filename) {

    // Why are we using XMLHttpRequest? See:
    // https://github.com/expo/expo/issues/2402#issuecomment-443726662
    const blob = await new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.onload = function () {
            resolve(xhr.response);
        };
        xhr.onerror = function (e) {
            console.error(e);
            reject(new TypeError("Network request failed"));
        };
        xhr.responseType = "blob";
        xhr.open("GET", uri, true);
        xhr.send(null);
    });

    const fileRef = ref(getStorage(), `${filename + fileExtension}`);    
    const result = await uploadBytes(fileRef, blob);

    // We're done with the blob, close and release it
    blob.close();

    return await getDownloadURL(fileRef);
}

I thought "maybe" firebase storage was not able to determine the size of the blob, so I also attempted to pass in size as a customMetadata by doing the following. Still doesnt work unfortunatelly.

await uploadBytes(fileRef, blob, {customMetadata: {size: blob.size}});

and change the security rule to something like

allow read, write: if request.resource.metadata.customMetadata.size < 99 * 1024 * 1024;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文