带有身份验证的 Flutter Firebase 上传
Flutter Firebase Upload with Authentication
我正在尝试使用以下内容以登录用户身份上传个人资料照片,但出现错误
E/flutter (32619): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_storage/unauthorized] User is not authorized to perform the desired action.
我的代码
User? user = await authenticationService.getCurrentUser();
// Create a Reference to the file
Reference ref =
FirebaseStorage.instance.ref().child(user!.uid).child('/profile.jpg');
final metadata = SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': s!.path});
UploadTask uploadTask = ref.putFile(File(s.path), metadata);
var result = await uploadTask;
print(result);
我的规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
}
}
我在这里错过了什么?
我了解到您想上传文件到以下参考资料
FirebaseStorage.instance.ref().child(user!.uid).child('/profile.jpg');
并在安全规则中检查执行上传的用户的id是否对应于user!.uid
的值。
在您的安全规则中,您有一个 request.auth.uid == userId
的检查,但在规则中没有任何地方定义什么是 userId
。你需要使用一个wildcard,如下:
service firebase.storage {
// The {bucket} wildcard indicates we match files in all Cloud Storage buckets
match /b/{bucket}/o {
match /{userId}/{imageId} {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
}
}
如果你只在这个“”中上传名为profile.jpg
的图片,你可以添加一个imageId == 'profile.jpg'
这样的检查,但你也可以将路径定义为:
match /{userId}/profile.jpg {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
我正在尝试使用以下内容以登录用户身份上传个人资料照片,但出现错误
E/flutter (32619): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_storage/unauthorized] User is not authorized to perform the desired action.
我的代码
User? user = await authenticationService.getCurrentUser();
// Create a Reference to the file
Reference ref =
FirebaseStorage.instance.ref().child(user!.uid).child('/profile.jpg');
final metadata = SettableMetadata(
contentType: 'image/jpeg',
customMetadata: {'picked-file-path': s!.path});
UploadTask uploadTask = ref.putFile(File(s.path), metadata);
var result = await uploadTask;
print(result);
我的规则
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
}
}
我在这里错过了什么?
我了解到您想上传文件到以下参考资料
FirebaseStorage.instance.ref().child(user!.uid).child('/profile.jpg');
并在安全规则中检查执行上传的用户的id是否对应于user!.uid
的值。
在您的安全规则中,您有一个 request.auth.uid == userId
的检查,但在规则中没有任何地方定义什么是 userId
。你需要使用一个wildcard,如下:
service firebase.storage {
// The {bucket} wildcard indicates we match files in all Cloud Storage buckets
match /b/{bucket}/o {
match /{userId}/{imageId} {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}
}
}
如果你只在这个“profile.jpg
的图片,你可以添加一个imageId == 'profile.jpg'
这样的检查,但你也可以将路径定义为:
match /{userId}/profile.jpg {
allow read;
allow write: if request.auth.uid == userId
&& request.resource.contentType.matches('image/.+');
}