上传内容时出现 Firebase 存储错误(错误?)

我创建了一个将照片上传到我的存储空间的功能。有时它工作正常,但在几分钟内我收到以下错误:


Possible Unhandled Promise Rejection (id: 0):

FirebaseStorageError {

  "code_": "storage/unauthorized",

  "message_": "Firebase Storage: User does not have permission to access 'photos/1d1be05f-b82d-4928-9a8e-002abc0d462e'.",

  "name_": "FirebaseError",

  "serverResponse_": "{

  \"error\": {

    \"code\": 403,

    \"message\": \"Permission denied. Could not perform this operation\"

  }

}",

}

我正在使用 Expo,并使用此函数获取本地 uri(用于创建 blob):


export function urlToBlob(url) {

  return new Promise((resolve, reject) => {

    var xhr = new XMLHttpRequest();

    xhr.onerror = reject;

    xhr.onreadystatechange = () => {

      if (xhr.readyState === 4) {

        resolve(xhr.response);

      }

    };

    xhr.open("GET", url);

    xhr.responseType = "blob"; // convert type

    xhr.send();

  });

}

 

我的存储安全规则如下所示:


rules_version = '2';

service firebase.storage {

  match /b/{bucket}/o {

    function isSignedIn() {

        return request.auth.uid != null;

    }

    match /photos/{photo} {

            function hasValidSize() {

        // Max. photo size = 30MB (For all dimensions)

        return request.resource.size < 30 * 1024 * 1024;

      }

      function isImage() {

            return request.resource.contentType.matches("image/.*");

            }

      allow read;

      allow write: if isSignedIn() && isImage() && hasValidSize();

    }

  }

}

我认为错误就在这里,因为当我删除 uploadTask.then 时工作正常。


// 上传到存储 const uploadTask = storageRef.put(blob);


uploadTask.on("state_changed", (taskSnapshot) => {

  // Update progress bar

  const percent =

    (taskSnapshot.bytesTransferred / taskSnapshot.totalBytes) * 100;


  setUploadProgress(percent);

});



// When the image is fully uploaded to the storage...

    uploadTask.then(async () => {  // <------------------------- When I delete this...

       ...   

      // Reset upload progress

      setUploadProgress(null);

    }); // <------------------------- ...and this, all works fine

  };

有任何想法吗?


更新


当我更改存储规则(删除 isImage() 条件)时,它也适用于 uploadTask。


宝慕林4294392
浏览 145回答 1
1回答

不负相思意

我认为这是 Expo 错误,因为我从开发模式转移到生产模式,一切正常!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript