// Create a root referenceStorageReferencestorageRef=storage.RootReference;// Create a reference to "mountains.jpg"StorageReferencemountainsRef=storageRef.Child("mountains.jpg");// Create a reference to 'images/mountains.jpg'StorageReferencemountainImagesRef=storageRef.Child("images/mountains.jpg");// While the file names are the same, the references point to different filesAssert.AreEqual(mountainsRef.Name,mountainImagesRef.Name);Assert.AreNotEqual(mountainsRef.Path,mountainImagesRef.Path);
// Data in memoryvarcustomBytes=newbyte[]{/*...*/};// Create a reference to the file you want to uploadStorageReferenceriversRef=storageRef.Child("images/rivers.jpg");// Upload the file to the path "images/rivers.jpg"riversRef.PutBytesAsync(customBytes).ContinueWith((Task<StorageMetadata>task)=>{if(task.IsFaulted||task.IsCanceled){Debug.Log(task.Exception.ToString());// Uh-oh, an error occurred!}else{// Metadata contains file metadata such as size, content-type, and md5hash.StorageMetadatametadata=task.Result;stringmd5Hash=metadata.Md5Hash;Debug.Log("Finished uploading...");Debug.Log("md5 hash = "+md5Hash);}});
// File located on diskstringlocalFile="...";// Create a reference to the file you want to uploadStorageReferenceriversRef=storageRef.Child("images/rivers.jpg");// Upload the file to the path "images/rivers.jpg"riversRef.PutFileAsync(localFile).ContinueWith((Task<StorageMetadata>task)=>{if(task.IsFaulted||task.IsCanceled){Debug.Log(task.Exception.ToString());// Uh-oh, an error occurred!}else{// Metadata contains file metadata such as size, content-type, and download URL.StorageMetadatametadata=task.Result;stringmd5Hash=metadata.Md5Hash;Debug.Log("Finished uploading...");Debug.Log("md5 hash = "+md5Hash);}});
// Create storage referenceStorageReferencemountainsRef=storageRef.Child("images/mountains.jpg");byte[]customBytes=newbyte[]{/*...*/};stringlocalFile="...";// Create file metadata including the content typevarnewMetadata=newMetadataChange();newMetadata.ContentType="image/jpeg";// Upload data and metadatamountainsRef.PutBytesAsync(customBytes,newMetadata,null,CancellationToken.None);// .ContinueWithOnMainThread(...// Upload file and metadatamountainsRef.PutFileAsync(localFile,newMetadata,null,CancellationToken.None);// .ContinueWithOnMainThread(...
// Start uploading a filevartask=storageRef.Child("images/mountains.jpg").PutFileAsync(localFile,null,newStorageProgress<UploadState>(state=>{// called periodically during the uploadDebug.Log(String.Format("Progress: {0} of {1} bytes transferred.",state.BytesTransferred,state.TotalByteCount));}),CancellationToken.None,null);task.ContinueWithOnMainThread(resultTask=>{if(!resultTask.IsFaulted && !resultTask.IsCanceled){Debug.Log("Upload finished.");}});
[[["易于理解","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-05。"],[],[],null,["\u003cbr /\u003e\n\nCloud Storage for Firebase allows you to quickly and easily upload files to a\n[Cloud Storage](//cloud.google.com/storage) bucket provided\nand 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| **Note:** For Spark plan projects, Firebase blocks upload and hosting of certain executable file types for Windows (files with `.exe`, `.dll` and `.bat` extensions), Android (`.apk` extension) and Apple (`.ipa` extension) by Cloud Storage for Firebase and Firebase Hosting. This policy exists to prevent abuse on our platform. Projects on the Blaze plan are not affected. For more information, see [this FAQ](/support/faq#storage-exe-restrictions).\n\nCreate a Reference\n\nTo upload a file, first\n[create a Cloud Storage reference](/docs/storage/unity/create-reference)\nto the file you want to upload.\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```c#\n// Create a root reference\nStorageReference storageRef = storage.RootReference;\n\n// Create a reference to \"mountains.jpg\"\nStorageReference mountainsRef = storageRef.Child(\"mountains.jpg\");\n\n// Create a reference to 'images/mountains.jpg'\nStorageReference mountainImagesRef =\n storageRef.Child(\"images/mountains.jpg\");\n\n// While the file names are the same, the references point to different files\nAssert.AreEqual(mountainsRef.Name, mountainImagesRef.Name);\nAssert.AreNotEqual(mountainsRef.Path, mountainImagesRef.Path);\n```\n\nYou cannot upload data with a reference to the root of your\nCloud Storage bucket. Your reference must point to a child URL.\n\nUpload Files\n\nOnce you have a reference, you can upload files to Cloud Storage\nin two ways:\n\n1. Upload from a byte array in memory\n2. Upload from a file path representing a file on device\n\nUpload from data in memory\n\nThe `PutBytesAsync()` method is the simplest way to upload a file to\nCloud Storage. `PutBytesAsync()` takes a byte\\[\\]\nand returns a `System.Task\u003cFirebase.Storage.StorageMetadata\u003e` which will\ncontain information about the file when the task completes. You can optionally\nuse an `IProgress\u003cUploadState\u003e` (typically `StorageProgress\u003cUploadState\u003e`) to\nmonitor your upload status. \n\n```c#\n// Data in memory\nvar customBytes = new byte[] {\n /*...*/\n};\n\n// Create a reference to the file you want to upload\nStorageReference riversRef = storageRef.Child(\"images/rivers.jpg\");\n\n// Upload the file to the path \"images/rivers.jpg\"\nriversRef.PutBytesAsync(customBytes)\n .ContinueWith((Task\u003cStorageMetadata\u003e task) =\u003e {\n if (task.IsFaulted || task.IsCanceled) {\n Debug.Log(task.Exception.ToString());\n // Uh-oh, an error occurred!\n }\n else {\n // Metadata contains file metadata such as size, content-type, and md5hash.\n StorageMetadata metadata = task.Result;\n string md5Hash = metadata.Md5Hash;\n Debug.Log(\"Finished uploading...\");\n Debug.Log(\"md5 hash = \" + md5Hash);\n }\n });\n```\n\nUpload from a local file\n\nYou can upload local files on the devices, such as photos and videos from the\ncamera, with the `PutFileAsync()` method. `PutFileAsync()` takes a `string`\nrepresenting the path to the file and returns a\n`System.Task\u003cFirebase.Storage.StorageMetadata\u003e` which will contain\ninformation about the file when the task completes. You can optionally\nuse an `IProgress\u003cUploadState\u003e` (typically `StorageProgress\u003cUploadState\u003e`) to\nmonitor your upload status. \n\n```c#\n// File located on disk\nstring localFile = \"...\";\n\n// Create a reference to the file you want to upload\nStorageReference riversRef = storageRef.Child(\"images/rivers.jpg\");\n\n// Upload the file to the path \"images/rivers.jpg\"\nriversRef.PutFileAsync(localFile)\n .ContinueWith((Task\u003cStorageMetadata\u003e task) =\u003e {\n if (task.IsFaulted || task.IsCanceled) {\n Debug.Log(task.Exception.ToString());\n // Uh-oh, an error occurred!\n }\n else {\n // Metadata contains file metadata such as size, content-type, and download URL.\n StorageMetadata metadata = task.Result;\n string md5Hash = metadata.Md5Hash;\n Debug.Log(\"Finished uploading...\");\n Debug.Log(\"md5 hash = \" + md5Hash);\n }\n });\n```\n\nIf you want to actively monitor your upload, you can use a `StorageProgress`\nclass or your own class that implements `IProgress\u003cUploadState\u003e`, with the\n`PutFileAsync()` or `PutBytesAsync()` methods.\nSee [Manage Uploads](#manage_uploads) for more information.\n\nAdd File Metadata\n\nYou can also include metadata when you upload files. This metadata contains\ntypical file metadata properties such as `Name`, `Size`, and `ContentType`\n(commonly referred to as MIME type). The `PutFileAsync()` method automatically\ninfers the content type from the filename extension, but you can override the\nauto-detected type by specifying `ContentType` in the metadata. If you do not\nprovide a `ContentType` and Cloud Storage cannot infer a default from\nthe file extension, Cloud Storage uses `application/octet-stream`. See\nthe [Use File Metadata](/docs/storage/unity/file-metadata)\nsection for more information about file metadata. \n\n```c#\n// Create storage reference\nStorageReference mountainsRef = storageRef.Child(\"images/mountains.jpg\");\n\nbyte[] customBytes = new byte[] {\n /*...*/\n};\nstring localFile = \"...\";\n\n// Create file metadata including the content type\nvar newMetadata = new MetadataChange();\nnewMetadata.ContentType = \"image/jpeg\";\n\n// Upload data and metadata\nmountainsRef.PutBytesAsync(customBytes, newMetadata, null,\n CancellationToken.None); // .ContinueWithOnMainThread(...\n// Upload file and metadata\nmountainsRef.PutFileAsync(localFile, newMetadata, null,\n CancellationToken.None); // .ContinueWithOnMainThread(...\n```\n\nMonitor Upload Progress\n\nYou can attach listeners to uploads in order to monitor the progress of the\nupload. The listener follows the standard `System.IProgress\u003cT\u003e`\ninterface. You can use an instance of the `StorageProgress` class, to provide\nyour own `Action\u003cT\u003e` as a callback for progress ticks. \n\n```c#\n// Start uploading a file\nvar task = storageRef.Child(\"images/mountains.jpg\")\n .PutFileAsync(localFile, null,\n new StorageProgress\u003cUploadState\u003e(state =\u003e {\n // called periodically during the upload\n Debug.Log(String.Format(\"Progress: {0} of {1} bytes transferred.\",\n state.BytesTransferred, state.TotalByteCount));\n }), CancellationToken.None, null);\n\ntask.ContinueWithOnMainThread(resultTask =\u003e {\n if (!resultTask.IsFaulted && !resultTask.IsCanceled) {\n Debug.Log(\"Upload finished.\");\n }\n});\n```\n\nError Handling\n\nThere are a number of reasons why errors may occur on upload, including\nthe local file not existing, or the user not having permission to upload\nthe desired file. You can find more information about errors in the\n[Handle Errors](/docs/storage/unity/handle-errors)\nsection of the docs.\n\nNext Steps\n\nNow that you've uploaded files, let's learn how to\n[download them](/docs/storage/unity/download-files)\nfrom Cloud Storage."]]