// Create a storage reference from our appfinalstorageRef=FirebaseStorage.instance.ref();// Create a reference with an initial file path and namefinalpathReference=storageRef.child("images/stars.jpg");// Create a reference to a file from a Google Cloud Storage URIfinalgsReference=FirebaseStorage.instance.refFromURL("gs://YOUR_BUCKET/images/stars.jpg");// Create a reference from an HTTPS URL// Note that in the URL, characters are URL escaped!finalhttpsReference=FirebaseStorage.instance.refFromURL("https://firebasestorage.googleapis.com/b/YOUR_BUCKET/o/images%20stars.jpg");
finalislandRef=storageRef.child("images/island.jpg");try{constoneMegabyte=1024*1024;finalUint8List?data=awaitislandRef.getData(oneMegabyte);// Data for "images/island.jpg" is returned, use this as needed.}onFirebaseExceptioncatch(e){// Handle any errors.}
finalislandRef=storageRef.child("images/island.jpg");finalappDocDir=awaitgetApplicationDocumentsDirectory();finalfilePath="${appDocDir.absolute}/images/island.jpg";finalfile=File(filePath);finaldownloadTask=islandRef.writeToFile(file);downloadTask.snapshotEvents.listen((taskSnapshot){switch(taskSnapshot.state){caseTaskState.running:// TODO: Handle this case.break;caseTaskState.paused:// TODO: Handle this case.break;caseTaskState.success:// TODO: Handle this case.break;caseTaskState.canceled:// TODO: Handle this case.break;caseTaskState.error:// TODO: Handle this case.break;}});
finalislandRef=storageRef.child("images/island.jpg");finalappDocDir=awaitgetApplicationDocumentsDirectory();finalfilePath="${appDocDir.absolute}/images/island.jpg";finalfile=File(filePath);finaldownloadTask=islandRef.writeToFile(file);downloadTask.snapshotEvents.listen((taskSnapshot){switch(taskSnapshot.state){caseTaskState.running:// TODO: Handle this case.break;caseTaskState.paused:// TODO: Handle this case.break;caseTaskState.success:// TODO: Handle this case.break;caseTaskState.canceled:// TODO: Handle this case.break;caseTaskState.error:// TODO: Handle this case.break;}});
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["\u003cbr /\u003e\n\nCloud Storage for Firebase allows you to quickly and easily download\nfiles from a [Cloud Storage](//cloud.google.com/storage)\nbucket provided and managed by Firebase.\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nCreate a Reference\n\nTo download a file, first [create a Cloud Storage reference](/docs/storage/flutter/create-reference)\nto the file you want to download.\n\nYou can create a reference by appending child paths to the root of your\nCloud Storage bucket, or you can create a reference from an existing\n`gs://` or `https://` URL referencing an object in Cloud Storage. \n\n // Create a storage reference from our app\n final storageRef = FirebaseStorage.instance.ref();\n\n // Create a reference with an initial file path and name\n final pathReference = storageRef.child(\"images/stars.jpg\");\n\n // Create a reference to a file from a Google Cloud Storage URI\n final gsReference =\n FirebaseStorage.instance.refFromURL(\"gs://YOUR_BUCKET/images/stars.jpg\");\n\n // Create a reference from an HTTPS URL\n // Note that in the URL, characters are URL escaped!\n final httpsReference = FirebaseStorage.instance.refFromURL(\n \"https://firebasestorage.googleapis.com/b/YOUR_BUCKET/o/images%20stars.jpg\");\n\nDownload Files\n\nOnce you have a reference, you can download files from Cloud Storage\nby calling the `getData()`. If you prefer to download the file\nwith another library, you can get a download URL with `getDownloadUrl()`.\n\nDownload in memory\n\nDownload the file to a `UInt8List` with the `getData()` method. This is the\neasiest way to download a file, but it must load the entire contents of\nyour file into memory. If you request a file larger than your app's available\nmemory, your app will crash. To protect against memory issues, `getData()`\ntakes a maximum amount of bytes to download. Set the maximum size to something\nyou know your app can handle, or use another download method. \n\n final islandRef = storageRef.child(\"images/island.jpg\");\n\n try {\n const oneMegabyte = 1024 * 1024;\n final Uint8List? data = await islandRef.getData(oneMegabyte);\n // Data for \"images/island.jpg\" is returned, use this as needed.\n } on FirebaseException catch (e) {\n // Handle any errors.\n }\n\nDownload to a local file\n\nThe `writeToFile()` method downloads a file directly to a local device. Use this if\nyour users want to have access to the file while offline or to share the file in a\ndifferent app. `writeToFile()` returns a `DownloadTask` which you can use to manage\nyour download and monitor the status of the download. \n\n final islandRef = storageRef.child(\"images/island.jpg\");\n\n final appDocDir = await getApplicationDocumentsDirectory();\n final filePath = \"${appDocDir.absolute}/images/island.jpg\";\n final file = File(filePath);\n\n final downloadTask = islandRef.writeToFile(file);\n downloadTask.snapshotEvents.listen((taskSnapshot) {\n switch (taskSnapshot.state) {\n case TaskState.running:\n // TODO: Handle this case.\n break;\n case TaskState.paused:\n // TODO: Handle this case.\n break;\n case TaskState.success:\n // TODO: Handle this case.\n break;\n case TaskState.canceled:\n // TODO: Handle this case.\n break;\n case TaskState.error:\n // TODO: Handle this case.\n break;\n }\n });\n\nDownload Data via URL\n\nIf you already have download infrastructure based around URLs, or just want\na URL to share, you can get the download URL for a file by calling the\n`getDownloadURL()` method on a Cloud Storage reference. \n\n final imageUrl =\n await storageRef.child(\"users/me/profile.png\").getDownloadURL();\n\nHandle Errors\n\nThere are a number of reasons why errors may occur on download, including the\nfile not existing, or the user not having permission to access the desired file.\nMore information on errors can be found in the [Handle Errors](/docs/storage/flutter/handle-errors)\nsection of the docs.\n\nFull Example\n\nA full example of a download with error handling is shown below: \n\n final islandRef = storageRef.child(\"images/island.jpg\");\n\n final appDocDir = await getApplicationDocumentsDirectory();\n final filePath = \"${appDocDir.absolute}/images/island.jpg\";\n final file = File(filePath);\n\n final downloadTask = islandRef.writeToFile(file);\n downloadTask.snapshotEvents.listen((taskSnapshot) {\n switch (taskSnapshot.state) {\n case TaskState.running:\n // TODO: Handle this case.\n break;\n case TaskState.paused:\n // TODO: Handle this case.\n break;\n case TaskState.success:\n // TODO: Handle this case.\n break;\n case TaskState.canceled:\n // TODO: Handle this case.\n break;\n case TaskState.error:\n // TODO: Handle this case.\n break;\n }\n });\n\nYou can also [get and update metadata](/docs/storage/flutter/file-metadata) for files that are stored\nin Cloud Storage."]]