Creating References in Cloud Storage
When we use Firebase Cloud Storage, all our files are stored in a Google Cloud Storage bucket. Just like the file system in our local hard disk, or the data in the Firebase Realtime Database, files are presented in a hierarchical structure in the bucket.
By creating a reference of a file, our app gains access:
- To upload or download data.
- To get or update metadata.
- To delete a file.
A reference can be either point to a specific file or to a higher level node in the hierarchy. The paths are very similar to the Firebase Realtime Database paths. The only difference is that our file data is stored in Google Cloud Storage, not in the Realtime Database.
Creating a Reference
The reference can be thought of as a pointer to a file in the cloud:
- Lightweight, so we can create as many as we need or require.
- Reusable for multiple operations.
The references are created using the FirebaseStorage singleton instance and will call it using the getReference() method.
Creating a Child Reference
We can create a reference to a location lower in the tree. For example, ‘images/season.jpg’ by using the getChild() method on an existing reference.
Child references will also take paths. The mountainRef now points to “images/mountain.jpg”, and imagesRef still points to “images”.
Navigating up in our file hierarchy
There are two ways to navigate up in our file hierarchy, i.e., getParent() and getRoot(). The getParent() navigates up one level, and the getRoot() navigates all the way to the top.
The parent allows us to move our reference to a parent node, and imagesRef now points to the ‘images’.
The root allows us to move all the way back to the top of our bucket, and rootRef now points to the root.
References can be chained together multiple times, mountainRef point to ‘images/mountain.jpg’.
If the nullRef is null then the parent of root is null
Inspecting References
We can inspect references to understand better the files they point to. The getPath(), getName(), and getBucket() methods will get the file’s full path, name, and bucket. The reference path is “images/space.jpg”, and it is analogous to the file name.
Reference’s bucket is the storage bucket where the files are stored in