Create a Cloud Storage reference with Cloud Storage for C++

Your files are stored in aCloud Storage bucket. Thefiles in this bucket are presented in a hierarchical structure, just like thefile system on your local hard disk, or the data in theFirebase Realtime Database.By creating a reference to a file, your app gains access to it. These referencescan then be used to upload or download data, get or update metadata or deletethe file. A reference can either point to a specific file or to a higher levelnode in the hierarchy.

If you've used theFirebase Realtime Database, these paths shouldseem very familiar to you. However, your file data is stored inCloud Storage,not in theRealtime Database.

Create a Reference

Create a reference to upload, download, or delete a file,or to get or update its metadata. A referencecan be thought of as a pointer to a file in the cloud. References arelightweight, so you can create as many as you need. They are also reusable formultiple operations.

References are created from thestorage service on your Firebase app bycalling theGetReferenceFromUrl() method and passing in a URL of the formgs://<your-cloud-storage-bucket>. You can find this URL in theStorage section of theFirebase console.

// Get a reference to the storage service, using the default Firebase AppStorage*storage=Storage::GetInstance(app);// Create aCloud Storage reference from our storage serviceStorageReferencestorage_ref=storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

You can create a reference to a location lower in the tree,say'images/space.jpg', by using thechild method on an existing reference.

// 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");

Navigate with References

You can also use theParent andRoot methods to navigate up in our filehierarchy.Parent navigates up one level, whileRoot navigates all the wayto the top.

// 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();

Child,Parent, andRoot can be chained together multiple times, aseach returns a reference. The exception is theParent ofRoot, whichis an invalidStorageReference.

// 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 Methods

You can inspect references to better understand the files they point to usingthefull_path,name, andbucket methods. These methods get the file's fullpath, name, and bucket.

// 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 theCloud Storage bucket where files are storedspace_ref.bucket();

Limitations on References

Reference paths and names can contain any sequence of valid Unicode characters,but certain restrictions are imposed including:

  1. Total length of reference.fullPath must be between 1 and 1024 bytes when UTF-8 encoded.
  2. No Carriage Return or Line Feed characters.
  3. Avoid using#,[,],*, or?, as these do not work well withother tools such as theFirebase Realtime Databaseorgsutil.

Full Example

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();

Next Steps

Next, let's learn how toupload files toCloud Storage.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.