1

I have the following dart code:

const String _csvFolder = "CSV_files/";
const String _reportFolder = "Report_files/";

Future<bool> writeFiles(
    String uuid, Uint8List csvBytes, Uint8List reportBytes) async {
  Reference csvFileRef = FirebaseStorage.instance.ref(_csvFolder + uuid);
  Reference reportFileRef = FirebaseStorage.instance.ref(_reportFolder + uuid);
  try {
    await csvFileRef.putData(csvBytes);
    await reportFileRef.putData(reportBytes);
    var test = await FirebaseStorage.instance.ref(_csvFolder + uuid).getData();
    return true;
  } catch (ex) {
    return false;
  }
}

and the following public firebase storage rules:

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

The lines

await csvFileRef.putData(csvBytes);
await reportFileRef.putData(reportBytes);

work and I can see the files on my storage. But the line

var test = await FirebaseStorage.instance.ref(_csvFolder + uuid).getData();

fails with an XMLHttpRequest exception.

But if I run flutter run -d chrome --web-browser-flag "--disable-web-security" as I saw at https://stackoverflow.com/a/74783428/11244991 then it works.

So it should be a problem with CORS rules if I understand well. Any solution to fix this ?

Kantine
  • 747
  • 6
  • 14

1 Answers1

1

I needed to configure my CORS rules with a cors.json file following this documentation:

https://firebase.google.com/docs/storage/web/download-files#cors_configuration

Kantine
  • 747
  • 6
  • 14