Cloud Storage

Cloud Storage for Firebase lets you upload and share user generated content, such as images and video, which allows you to build rich media content into your apps. Your data is stored in a Google Cloud Storage bucket — an exabyte scale object storage solution with high availability and global redundancy. Cloud Storage for Firebase lets you securely upload these files directly from mobile devices and web browsers, handling spotty networks with ease.

Get Started

Create a default Cloud Storage bucket

  1. From the navigation pane of the Firebase console, select Storage, then click Get started.

  2. Review the messaging about securing your Cloud Storage data using security rules. During development, consider setting up your rules for public access.

  3. Select a location for your default Cloud Storage bucket.

    • This location setting is your project's default Google Cloud Platform (GCP) resource location. Note that this location will be used for GCP services in your project that require a location setting, specifically, your Cloud Firestore database and your App Engine app (which is required if you use Cloud Scheduler).
    • If you aren't able to select a location, then your project already has a default GCP resource location. It was set either during project creation or when setting up another service that requires a location setting.

    If you're on the Blaze plan, you can create multiple buckets, each with its own location.

Warning: After you set your project's default GCP resource location, you cannot change it.

  1. Click Done.

Set up public access

Cloud Storage for Firebase provides a declarative rules language that allows you to define how your data should be structured, how it should be indexed, and when your data can be read from and written to. By default, read and write access to Cloud Storage is restricted so only authenticated users can read or write data. To get started without setting up Authentication, you can configure your rules for public access.

This does make Cloud Storage open to anyone, even people not using your app, so be sure to restrict your Cloud Storage again when you set up authentication.

Create a Cloud Storage reference

Create a Reference

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

References are created from the storage service on your Firebase app by calling the GetReferenceFromUrl() method and passing in a URL of the form gs://<your-cloud-storage-bucket>. You can find this URL in the Storage section of the Firebase console.

BlueprintsC++
// C++ code example not available yet.

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

BlueprintsC++
// C++ code example not available yet.

You can also use the Parent and Root methods to navigate up in our file hierarchy. Parent navigates up one level, while Root navigates all the way to the top.

Child, Parent, and Root can be chained together multiple times, as each returns a reference. The exception is the Parent of Root, which is an invalid StorageReference.

Upload Files

Once you have a reference, you can upload files to Cloud Storage in two ways:

  1. Upload from a byte buffer in memory
  2. Upload from a file path representing a file on device

Upload from data in memory

The PutBytes() method is the simplest way to upload a file to Cloud Storage. PutBytes() takes a byte buffer and return a Metadata which will contain information about the file when the upload completes. You can use a Controller to manage your upload and monitor its status.

BlueprintsC++
// C++ code example not available yet.

Upload from a local file

You can upload local files on the devices, such as photos and videos from the camera, with the PutFile() method. PutFile() takes a FString representing the path to the file and returns a Metadata which will contain information about the file when the upload completes. You can use Controller to manage your upload and monitor its status.

BlueprintsC++
// C++ code example not available yet.

Download Files

Once you have a reference, you can download files from Cloud Storage in three ways:

  1. Download to a buffer in memory
  2. Download to an specific path on the device
  3. Generate an string URL representing the file online

Download in memory

Download the file to a byte buffer in memory using the GetBytes() method. This is the easiest way to quickly download a file, but it must load entire contents of your file into memory. If you request a file larger than your app's available memory, your app will crash. To protect against memory issues, make sure to set the max size to something you know your app can handle, or use another download method.

BlueprintsC++
// C++ code example not available yet.

Download to a local file

The GetFile() method downloads a file directly to a local device. Use this if your users want to have access to the file while offline or to share in a different app. GetFile() returns a Controller which you can use to manage your download.

BlueprintsC++
// C++ code example not available yet.

Generate a download URL

If you already have download infrastructure based around URLs, or just want a URL to share, you can get the download URL for a file by calling the GetDownloadUrl() method on a Cloud Storage reference.

BlueprintsC++
// C++ code example not available yet.