我想从我的 flutter 应用程序将大型视频文件上传到 firebase 存储

问题描述 投票:0回答:1

这是上传的方式,代码中也有

flutterflow
功能,因为它是从它迁移而来的,我逐渐放弃它的功能,我也检查了
firebase
存储规则

onPressed: () async {
  final selectedMedia = await selectMedia(
    isVideo: true,
    mediaSource: MediaSource.videoGallery,
    multiImage: false,
  );
  if (selectedMedia != null && selectedMedia.every((m) =>
    validateFileFormat(
    m.storagePath, context))) {
      setState(() => _model.isDataUploading1 = true);
      var selectedUploadedFiles = <FFUploadedFile>[];

      var downloadUrls = <String>[];
      try {
      showUploadMessage(
      context,
      'Uploading file...',
      showLoading: true,
      );
      selectedUploadedFiles = selectedMedia
      .map((m) => FFUploadedFile(
      name: m.storagePath
      .split('/')
      .last,
      bytes: m.bytes,
      height: m.dimensions?.height,
      width: m.dimensions?.width,
      blurHash: m.blurHash,
      ))
      .toList();

      downloadUrls = (await Future.wait(
      selectedMedia.map(
      (m) async => await uploadData(
      m.storagePath, m.bytes),
      ),
    ))
    .where((u) => u != null)
    .map((u) => u!)
    .toList();
    } finally { ScaffoldMessenger.of(context).hideCurrentSnackBar();
    _model.isDataUploading1 = false;
    }
    if (selectedUploadedFiles.length == selectedMedia.length &&     downloadUrls.length == selectedMedia.length) {
       setState(() {_model.uploadedLocalFile1 = selectedUploadedFiles.first;
    _model.uploadedFileUrl1 = downloadUrls.first;
    });
    showUploadMessage(context, 'Success!');
    } else {
      setState(() {});
      showUploadMessage(context, 'Failed to upload data');
        return;
    }
  }
},

这是存储规则

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if false;
    }

    // Allow public read access to PDF files
    match /pdfs/{pdfFile} {
      allow read: if request.auth != null && 
                   pdfFile.matches('.*\\.pdf');
    }
    
    // Maintain existing user-specific rules
    match /users/{userId}/{allPaths=**} {
      allow read: if request.auth.uid == userId;
      allow write: if request.auth.uid == userId;
    }
  }
}

我尝试使用 700mb 的视频文件进行测试,但遇到了一段时间的加载卡住,然后我再次能够选择视频进行上传,我猜任务已终止,我还可以提供一些调试日志

flutter firebase dart
1个回答
0
投票

放宽 Firebase 存储规则即可解决问题

© www.soinside.com 2019 - 2024. All rights reserved.