我创建了一个将照片上传到我的存储空间的功能。有时它工作正常,但在几分钟内我收到以下错误: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。
添加回答
举报
0/150
提交
取消
