// Get a reference to the storage service, using the default Firebase AppStorage*storage=Storage::GetInstance(app);// Create a Cloud Storage reference from our storage serviceStorageReferencestorage_ref=storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
// Create a child reference// images_ref now points to "images"StorageReferenceimages_ref=storage_ref.Child("images");// Child references can also take paths delimited by '/'// space_ref now points to "images/space.jpg"// images_ref still points to "images"StorageReferencespace_ref=storage_ref.Child("images/space.jpg");// This is equivalent to creating the full referenceStorageReferencespace_ref=storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");
// Parent allows us to move to the parent of a reference// images_ref now points to 'images'StorageReferenceimages_ref=space_ref.Parent();// Root allows us to move all the way back to the top of our bucket// root_ref now points to the rootStorageReferenceroot_ref=space_ref.Root();
// References can be chained together multiple times// earth_ref points to "images/earth.jpg"StorageReferenceearth_ref=space_ref.Parent().Child("earth.jpg");// null_ref is null, since the parent of root is an invalid StorageReferenceStorageReferencenull_ref=space_ref.Root().Parent();
// Reference's path is: "images/space.jpg"// This is analogous to a file path on diskspace_ref.full_path();// Reference's name is the last segment of the full path: "space.jpg"// This is analogous to the file namespace_ref.name();// Reference's bucket is the name of the Cloud Storage bucket where files are storedspace_ref.bucket();
Storage*storage=Storage::GetInstance(app);// Points to the root referenceStorageReferencestorage_ref=storage->GetReferenceFromUrl("gs://<your-bucket-name>");// Points to "images"StorageReferenceimages_ref=storage_ref.Child("images");// Points to "images/space.jpg"// Note that you can use variables to create child valuesstd::stringfilename="space.jpg";StorageReferencespace_ref=images_ref.Child(filename);// File path is "images/space.jpg"std::stringpath=space_ref.full_path()// File name is "space.jpg"std::stringname=space_ref.name()// Points to "images"StorageReferenceimages_ref=space_ref.Parent();
[[["易于理解","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-08-16。"],[],[],null,["\u003cbr /\u003e\n\nYour files are stored in a\n[Cloud Storage](//cloud.google.com/storage) bucket. The\nfiles in this bucket are presented in a hierarchical structure, just like the\nfile system on your local hard disk, or the data in the Firebase Realtime Database.\nBy creating a reference to a file, your app gains access to it. These references\ncan then be used to upload or download data, get or update metadata or delete\nthe file. A reference can either point to a specific file or to a higher level\nnode in the hierarchy.\n\nIf you've used the [Firebase Realtime Database](/docs/database), these paths should\nseem very familiar to you. However, your file data is stored in\nCloud Storage, **not** in the Realtime Database.\n\nCreate a Reference\n\nCreate a reference to upload, download, or delete a file,\nor to get or update its metadata. A reference\ncan be thought of as a pointer to a file in the cloud. References are\nlightweight, so you can create as many as you need. They are also reusable for\nmultiple operations.\n\nReferences are created from the `storage` service on your Firebase app by\ncalling the `GetReferenceFromUrl()` method and passing in a URL of the form\n`gs://\u003cyour-cloud-storage-bucket\u003e`. You can find this URL in the\n*Storage* section of the [Firebase console](//console.firebase.google.com/). \n\n```c++\n// Get a reference to the storage service, using the default Firebase App\nStorage* storage = Storage::GetInstance(app);\n\n// Create a Cloud Storage reference from our storage service\nStorageReference storage_ref = storage-\u003eGetReferenceFromUrl(\"gs://\u003cyour-cloud-storage-bucket\u003e\");\n```\n\nYou can create a reference to a location lower in the tree,\nsay `'images/space.jpg'`, by using the `child` method on an existing reference. \n\n```c++\n// Create a child reference\n// images_ref now points to \"images\"\nStorageReference images_ref = storage_ref.Child(\"images\");\n\n// Child references can also take paths delimited by '/'\n// space_ref now points to \"images/space.jpg\"\n// images_ref still points to \"images\"\nStorageReference space_ref = storage_ref.Child(\"images/space.jpg\");\n\n// This is equivalent to creating the full reference\nStorageReference space_ref = storage.GetReferenceFromUrl(\"gs://\u003cyour-cloud-storage-bucket\u003e/images/space.jpg\");\n```\n\nNavigate with References\n\nYou can also use the `Parent` and `Root` methods to navigate up in our file\nhierarchy. `Parent` navigates up one level, while `Root` navigates all the way\nto the top. \n\n```c++\n// Parent allows us to move to the parent of a reference\n// images_ref now points to 'images'\nStorageReference images_ref = space_ref.Parent();\n\n// Root allows us to move all the way back to the top of our bucket\n// root_ref now points to the root\nStorageReference root_ref = space_ref.Root();\n```\n\n`Child`, `Parent`, and `Root` can be chained together multiple times, as\neach returns a reference. The exception is the `Parent` of `Root`, which\nis an invalid `StorageReference`. \n\n```c++\n// References can be chained together multiple times\n// earth_ref points to \"images/earth.jpg\"\nStorageReference earth_ref = space_ref.Parent().Child(\"earth.jpg\");\n\n// null_ref is null, since the parent of root is an invalid StorageReference\nStorageReference null_ref = space_ref.Root().Parent();\n```\n\nReference Methods\n\nYou can inspect references to better understand the files they point to using\nthe `full_path`, `name`, and `bucket` methods. These methods get the file's full\npath, name, and bucket. \n\n```c++\n// Reference's path is: \"images/space.jpg\"\n// This is analogous to a file path on disk\nspace_ref.full_path();\n\n// Reference's name is the last segment of the full path: \"space.jpg\"\n// This is analogous to the file name\nspace_ref.name();\n\n// Reference's bucket is the name of the Cloud Storage bucket where files are stored\nspace_ref.bucket();\n```\n\nLimitations on References\n\nReference paths and names can contain any sequence of valid Unicode characters,\nbut certain restrictions are imposed including:\n\n1. Total length of reference.fullPath must be between 1 and 1024 bytes when UTF-8 encoded.\n2. No Carriage Return or Line Feed characters.\n3. Avoid using `#`, `[`, `]`, `*`, or `?`, as these do not work well with other tools such as the [Firebase Realtime Database](/docs/database) or [gsutil](https://cloud.google.com/storage/docs/gsutil).\n\nFull Example \n\n```c++\nStorage* storage = Storage::GetInstance(app);\n\n// Points to the root reference\nStorageReference storage_ref = storage-\u003eGetReferenceFromUrl(\"gs://\u003cyour-bucket-name\u003e\");\n\n// Points to \"images\"\nStorageReference images_ref = storage_ref.Child(\"images\");\n\n// Points to \"images/space.jpg\"\n// Note that you can use variables to create child values\nstd::string filename = \"space.jpg\";\nStorageReference space_ref = images_ref.Child(filename);\n\n// File path is \"images/space.jpg\"\nstd::string path = space_ref.full_path()\n\n// File name is \"space.jpg\"\nstd::string name = space_ref.name()\n\n// Points to \"images\"\nStorageReference images_ref = space_ref.Parent();\n```\n\nNext Steps\n\nNext, let's learn how to\n[upload files](/docs/storage/cpp/upload-files) to\nCloud Storage."]]