我創建了一個將照片上傳到我的存儲空間的功能。有時它工作正常,但在幾分鐘內我收到以下錯誤: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。
上傳內容時出現 Firebase 存儲錯誤(錯誤?)
寶慕林4294392
2022-11-03 14:57:48
