Cloud Storage 버킷의 루트에 하위 경로를 추가하여 참조를 만들거나 Cloud Storage의 객체를 참조하는 기존 gs:// 또는 https:// URL로 참조를 만들 수 있습니다.
// 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");
파일 다운로드
참조를 만들었으면 getData()를 호출하여 Cloud Storage에서 파일을 다운로드할 수 있습니다. 다른 라이브러리로 파일을 다운로드하려는 경우 getDownloadUrl()로 다운로드 URL을 가져올 수 있습니다.
메모리에 다운로드
getData() 메서드로 파일을 UInt8List에 다운로드합니다. 이는 파일을 다운로드하는 가장 쉬운 방법이지만 메모리에 전체 파일 콘텐츠를 로드해야 합니다. 따라서 앱의 가용 메모리보다 큰 파일을 요청하면 앱이 다운됩니다. 메모리 문제를 방지하기 위해 getData()는 다운로드할 최대 바이트 수를 지정합니다. 최대 크기를 앱에서 처리할 수 있는 크기로 설정하거나 다른 다운로드 방법을 사용하세요.
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.}
로컬 파일로 다운로드
writeToFile() 메서드는 파일을 로컬 기기로 직접 다운로드합니다. 사용자가 오프라인 상태에서도 파일에 액세스해야 하거나 다른 앱에서 파일을 공유해야 하는 경우 이 방법을 사용하세요. writeToFile()은 다운로드를 관리하고 다운로드 상태를 모니터링하는 데 사용할 수 있는 DownloadTask를 반환합니다.
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;}});
URL을 통해 데이터 다운로드
URL 기반 다운로드 인프라를 갖추고 있거나 단순히 URL을 공유하려는 경우 Cloud Storage 참조에 getDownloadURL() 메서드를 호출하여 파일의 다운로드 URL을 가져올 수 있습니다.
다운로드 시 오류가 발생하는 이유는 파일이 없는 경우, 사용자에게
파일 액세스 권한이 없는 경우 등 다양합니다.
오류의 자세한 내용은 문서의 오류 처리 섹션을 참조하세요.
전체 예시
다음은 오류 처리가 포함된 다운로드에 대한 전체 예시입니다.
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"]],["최종 업데이트: 2025-09-04(UTC)"],[],[],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."]]