在Flutter中访问Firebase存储 [英] Access to Firebase Storage in Flutter
问题描述
我对Flutter相当陌生,之前从未使用过Firebase,因此如果对此有明显的解决方案,我深表歉意.
I'm fairly new to Flutter and haven't ever used Firebase before so my aplogies if there is an obvious solution to this.
我正在开发Flutter应用,该应用涉及记录表单提交并将其提交到中心位置.
I'm working on a Flutter app which involves recording form submissions and submitting them to a central location.
Firebase Storage似乎非常合适,因为据我了解,该应用程序可以将文件上传到云存储桶,然后可以通过Firebase控制台访问它们.如我错了请纠正我.
Firebase Storage seemed like a good fit since as I understood it the app can upload files to the cloud bucket and then they can be accessed through the Firebase console. Correct me if I'm wrong.
所以我找到了Flutter的这个模块;
https://pub.dartlang.org/packages/firebase_storage
So I found this module for Flutter;
https://pub.dartlang.org/packages/firebase_storage
我以示例代码为基础.
I used the example code as a basis.
一次在我调用的代码开始;
once at the start of the code I call;
final FirebaseApp app = await FirebaseApp.configure(
name: 'test',
options: new FirebaseOptions(
googleAppID: Platform.isIOS
? '{ios app id}'
: '{android app ID}',
gcmSenderID: '{project number code}',
apiKey: '{web api key from the firebase console}',
projectID: '{project ID}',
),
);
storage = new FirebaseStorage(
app: app, storageBucket: '{address to data bucket}');
然后上传我尝试过的文件;
Then to upload a file I've tried;
final StorageReference ref =
storage.ref().child('uploads').child(filename);
final StorageUploadTask uploadTask = ref.putFile(
file,
new StorageMetadata(
contentLanguage: 'en',
customMetadata: <String, String>{'activity': 'submission'},
),
);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
final http.Response downloadData = await http.get(downloadUrl);
final String name = await ref.getName();
final String bucket = await ref.getBucket();
final String path = await ref.getPath();
return downloadData.statusCode >= 200 && 299 >= downloadData.statusCode;
,并且在Firebase控制台中,我创建了Uploads文件夹,并认为其他所有设置都正确设置了.
and in the Firebase console I created the Uploads folder and I think set everything else up correctly.
我的Firebase存储访问规则如下所示;
My Firebase storage access rules look like this;
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
我的问题是,当我尝试上传文件时,出现以下错误;
My issue is that when I try to upload the file I get the following error;
W/DynamiteModule(25832): Local module descriptor class for com.google.android.gms.firebasestorage not found.
I/DynamiteModule(25832): Considering local module com.google.android.gms.firebasestorage:0 and remote module com.google.android.gms.firebasestorage:6
I/DynamiteModule(25832): Selected remote version of com.google.android.gms.firebasestorage, version >= 6
W/System (25832): ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000040/n/armeabi-v7a
W/System (25832): ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000040/n/armeabi
E/StorageUtil(25832): error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.
W/NetworkRequest(25832): no auth token for request
E/StorageException(25832): StorageException has occurred.
E/StorageException(25832): User does not have permission to access this object.
E/StorageException(25832): Code: -13021 HttpResult: 403
E/StorageException(25832): The server has terminated the upload session
E/StorageException(25832): java.io.IOException: The server has terminated the upload session
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzs(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzr(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.run(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.StorageTask.zzl(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.zzq.run(Unknown Source)
E/StorageException(25832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
E/StorageException(25832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
E/StorageException(25832): at java.lang.Thread.run(Thread.java:818)
E/StorageException(25832): Caused by: java.io.IOException: { "error": { "code": 403, "message": "Permission denied. Could not perform this operation" }}
E/StorageException(25832): at bha.a(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):147)
E/StorageException(25832): at bha.a(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):119)
E/StorageException(25832): at bgu.onTransact(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):7)
E/StorageException(25832): at android.os.Binder.transact(Binder.java:387)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zza.transactAndReadExceptionReturnVoid(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzm.zzf(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzq.zza(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzf.zza(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzc(Unknown Source)
E/StorageException(25832): ... 6 more
E/StorageException(25832): StorageException has occurred.
E/StorageException(25832): User does not have permission to access this object.
E/StorageException(25832): Code: -13021 HttpResult: 403
E/StorageException(25832): The server has terminated the upload session
E/StorageException(25832): java.io.IOException: The server has terminated the upload session
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzs(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzr(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.run(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.StorageTask.zzl(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.zzq.run(Unknown Source)
E/StorageException(25832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
E/StorageException(25832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
E/StorageException(25832): at java.lang.Thread.run(Thread.java:818)
E/StorageException(25832): Caused by: java.io.IOException: { "error": { "code": 403, "message": "Permission denied. Could not perform this operation" }}
E/StorageException(25832): at bha.a(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):147)
E/StorageException(25832): at bha.a(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):119)
E/StorageException(25832): at bgu.onTransact(:com.google.android.gms.dynamite_dynamitemodulesc@12685022@12.6.85 (040308-197041431):7)
E/StorageException(25832): at android.os.Binder.transact(Binder.java:387)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zza.transactAndReadExceptionReturnVoid(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzm.zzf(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzq.zza(Unknown Source)
E/StorageException(25832): at com.google.android.gms.internal.firebase_storage.zzf.zza(Unknown Source)
E/StorageException(25832): at com.google.firebase.storage.UploadTask.zzc(Unknown Source)
E/StorageException(25832): ... 6 more
是否可以使用应用程序帐户(服务帐户)登录,所以单个用户不需要登录?
Is it possible to log in with an app account (service account?) so individual users do not need to log in?
我在这里做什么错了?
推荐答案
从您的 StorageException
报价:
用户无权访问此对象.
User does not have permission to access this object.
这是您期望的,因为您的规则声明以下内容:
This is what you would expect because your rules state the following:
allow read, write: if request.auth != null;
Firebase 使用身份验证.
有两个选项可以解决此问题:
Either you authenticate your users with Firebase using the
firebase_auth
plugin
或者您可以通过将规则更改为allow read, write;
Or you make your files publicly available by changing your rules to allow read, write;
存储规则文档包含有关集成的信息Firebase身份验证.
这篇关于在Flutter中访问Firebase存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!