我正在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中.

为什么用户无权上传压缩文件?

推荐答案

您的安全规则允许用户写入以/users/<their user ID>开头的路径.

但是您的代码不遵循这些规则,并且试图写入/pics.zip.由于这不符合您自己的规则,因此写入被拒绝.

要解决这个问题,请将picRef设置为:

if (FirebaseAuth.instance.currentUser != null) {
  final uid = FirebaseAuth.instance.currentUser!.uid;
  final picRef = storageRef.child("/users/$uid/pics.zip");
  ...
}

Flutter相关问答推荐

底部导航栏项目在抖动中不更改 colored颜色

Dexing时出错.将Flutter 升级到3.19.0后

在Android Studio的新用户界面中未找到热重新加载

如何在默认情况下隐藏appBar并在向下滚动时显示它- Flutter

我有一个问题:类型';()=>;Null';不是类型转换中类型';(Int)=>;void';的子类型

为什么用户界面不会在Flight中的复选框中更改?

不要跨同步间隔使用BuildContext

升级到 Flutter 3.10 后,Flutter 键盘填充不起作用. Flutter 3.10 如何防止 BottomSheet 被键盘覆盖?

将状态维护到 asyncNotifier

在 flutter 中获取最喜欢的文档

启动画面中的淡入淡出图像

如果不刷新页面,我看不到从 Firestore 中提取的数据

为什么 appBar 小部件在 flutter 中不是常量

如何格式化网址?

Android Studio - Electric Eel 2022.1.1 更新 destruct 了 dart 文件图标

在 flutter 的另一个类中使用变量值

使用 onPressed 切换到另一个屏幕无法正常工作

在 Flutter 中将提取的 Listview.builder 与两个列表一起使用

Flutter:font_awesome_icon pro 版报错?如何设置 font_awesome 克隆仓库的路径?

更新未知文档 ID firebase\flutter 中的字段