我正在try 编写与Firebase交互的DART代码,这样代码就可以上传用户上传到云存储的照片的压缩文件.以下是我的代码:
final tempDir = await getTemporaryDirectory();
final zipPath = '${tempDir.path}/trainingData.zip';
final zipFile = await File(zipPath).writeAsBytes(encodedArchive);
final storage = FirebaseStorage.instance;
final storageRef = FirebaseStorage.instance.ref();
final picRef = storageRef.child("pics.zip");
final picCollectionsRef =
storageRef.child("picCollections/pics.zip");
assert(picRef.name == picCollectionsRef.name);
assert(picRef.fullPath != picCollectionsRef.fullPath);
try {
await picRef.putFile(
zipFile,
SettableMetadata(
contentType: "application/zip",
));
} catch (e) {
print('Error: Zip File not uploaded correctly - $e');
}
var downloadURL = await trainingDataRef.getDownloadURL();
return downloadURL;
在我的iPhone上测试这款应用时,我不断收到以下错误信息:
flutter: Error: Zip File not uploaded correctly - [firebase_storage/unauthorized] User is not authorized to perform the desired action.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: [firebase_storage/unauthorized] User is not authorized to perform the desired action.
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:651:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
<asynchronous suspension>
#2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:534:43)
<asynchronous suspension>
#3 MethodChannelReference.getDownloadURL (package:firebase_storage_platform_interface/src/method_channel/method_channel_reference.dart:45:36)
<asynchronous suspension>
以下是我的存储安全规则:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth.uid == userId;
}
match /users/{userId}/{allPaths=**} {
allow read: if true;
allow write: if request.auth.uid == userId;
}
}
}
出于安全原因,我不想更改规则,允许任何未经身份验证的人进行读写.当我测试时,用户已完全登录到Firebase,并且他们的帐户存在于Firebase中.
为什么用户无权上传压缩文件?