我正在开发一个使用 Vertex AI for Firebase SDK 的 Flutter 应用程序。当我仅使用文本提示而没有任何 Firebase 存储 URL 时,一切正常。但是,当包含 Firebase 存储 URL 时,我收到以下错误消息:调用者没有权限。
我发现将 Firebase Storage 安全规则更改为以下内容可以解决问题,但这些规则并不安全:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if true;
}
}
}
Stream<String?> generateTextFromMediaStream(String promptText, List<String>? mediaPaths) async* {
final prompt = TextPart(promptText);
List mediaPartsFuture = [];
if (mediaPaths != null) {
mediaPartsFuture = mediaPaths.map((path) {
final mimeType = getMimeType(path);
return FileData(mimeType, path);
}).toList();
}
final response = _model.generateContentStream([
Content.multi([prompt, ...mediaPartsFuture])
]);
await for (final chunk in response) {
yield chunk.text;
}
}
I/flutter (12483): The caller does not have permission
I/flutter (12483): #0 parseGenerateContentResponse (package:google_generative_ai/src/api.dart:558:54)
I/flutter (12483): #1 _MapStream._handleData (dart:async/stream_pipe.dart:213:31)
I/flutter (12483): #2 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
I/flutter (12483): #3 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
I/flutter (12483): #4 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:365:11)
I/flutter (12483): #5 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:297:7)
I/flutter (12483): #6 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:784:19)
I/flutter (12483): #7 _StreamController._add (dart:async/stream_controller.dart:658:7)
I/flutter (12483): #8 _StreamController.add (dart:async/stream_controller.dart:606:5)
I/flutter (12483): #9 _AsyncStarStreamController.add (dart:async-patch/async_patch.dart:76:16)
I/flutter (12483): #10 HttpApiClient.streamRequest (package:google_generative_ai/src/client.dart)
I/flutter (12483): <asynchronous suspension>
I/flutter (12483): #11 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:152:3)
I/flutter (12483): <asynchronous suspension>
我在 Google Cloud APIS 中看到以下错误:
Firebase ML API:
Flutter 3.22.0 firebase_vertexai 0.1.0+1
有人可以指导我了解可能导致此权限错误的原因以及如何在不影响 Firebase 存储规则安全性的情况下解决该问题吗?
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null || request.auth.token.email.matches('.*gserviceaccount.*') ||
request.auth.token.email == '[email protected]'||
request.auth.token.email == 'service-PROJECT_NUMBER@gcp-sa-aiplatform-cc.iam.gserviceaccount.com'||
request.auth.token.email == '[email protected]'||
request.auth.token.email == '[email protected]'||
request.auth.token.email == '[email protected]' ||
request.auth.token.email == '[email protected]' ||
request.auth.token.email == 'service-PROJECT_NUMBER@gcp-sa-firebasestorage.iam.gserviceaccount.com';
}
}
}
我将 PROJECT_NUMBER 替换为我的项目编号
您可以将 Firebase 身份验证添加到您的 Flutter 应用程序并实现该服务提供的登录提供程序之一(电子邮件/密码、Google 登录等)。
如果您不想询问用户的凭据,您可以实施匿名登录。
当您在应用中使用 Firebase Auth SDK 并登录用户时,它可确保对 Vertex AI 的所有调用均使用该用户的令牌进行身份验证。然后您可以将安全规则更改回原来的状态
request.auth != null
:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
如果您想更精细地控制安全规则(例如,用户只能读/写自己的文件),您可以查看有关 存储安全规则的 Firebase 文档。