Catch up on everything we announced at this year's Firebase Summit. Learn more

Upload files with Cloud Storage for Unity

Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.

Create a Reference

To upload a file, first create a Cloud Storage reference to the file you want to upload.

You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.

// Create a root reference
StorageReference storageRef = storage.RootReference;

// Create a reference to "mountains.jpg"
StorageReference mountainsRef = storageRef.Child("mountains.jpg");

// Create a reference to 'images/mountains.jpg'
StorageReference mountainImagesRef =
    storageRef.Child("images/mountains.jpg");

// While the file names are the same, the references point to different files
Assert.AreEqual(mountainsRef.Name, mountainImagesRef.Name);
Assert.AreNotEqual(mountainsRef.Path, mountainImagesRef.Path);

You cannot upload data with a reference to the root of your Cloud Storage bucket. Your reference must point to a child URL.

Upload Files

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

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

Upload from data in memory

The PutBytesAsync() method is the simplest way to upload a file to Cloud Storage. PutBytesAsync() takes a byte[] and returns a System.Task<Firebase.Storage.StorageMetadata> which will contain information about the file when the task completes. You can optionally use an IProgress<UploadState> (typically StorageProgress<UploadState>) to monitor your upload status.

// Data in memory
var customBytes = new byte[] {
    /*...*/
};

// Create a reference to the file you want to upload
StorageReference riversRef = 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.
            StorageMetadata metadata = task.Result;
            string md5Hash = metadata.Md5Hash;
            Debug.Log("Finished uploading...");
            Debug.Log("md5 hash = " + md5Hash);
        }
    });

Upload from a local file

You can upload local files on the devices, such as photos and videos from the camera, with the PutFileAsync() method. PutFileAsync() takes a string representing the path to the file and returns a System.Task<Firebase.Storage.StorageMetadata> which will contain information about the file when the task completes. You can optionally use an IProgress<UploadState> (typically StorageProgress<UploadState>) to monitor your upload status.

// File located on disk
string localFile = "...";

// Create a reference to the file you want to upload
StorageReference riversRef = 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.
            StorageMetadata metadata = task.Result;
            string md5Hash = metadata.Md5Hash;
            Debug.Log("Finished uploading...");
            Debug.Log("md5 hash = " + md5Hash);
        }
    });

If you want to actively monitor your upload, you can use a StorageProgress class or your own class that implements IProgress<UploadState>, with the PutFileAsync() or PutBytesAsync() methods. See Manage Uploads for more information.

Add File Metadata

You can also include metadata when you upload files. This metadata contains typical file metadata properties such as Name, Size, and ContentType (commonly referred to as MIME type). The PutFileAsync() method automatically infers the content type from the filename extension, but you can override the auto-detected type by specifying ContentType in the metadata. If you do not provide a ContentType and Cloud Storage cannot infer a default from the file extension, Cloud Storage uses application/octet-stream. See the Use File Metadata section for more information about file metadata.

// Create storage reference
StorageReference mountainsRef = storageRef.Child("images/mountains.jpg");

byte[] customBytes = new byte[] {
    /*...*/
};
string localFile = "...";

// Create file metadata including the content type
var newMetadata = new MetadataChange();
newMetadata.ContentType = "image/jpeg";

// Upload data and metadata
mountainsRef.PutBytesAsync(customBytes, newMetadata, null,
    CancellationToken.None); // .ContinueWithOnMainThread(...
// Upload file and metadata
mountainsRef.PutFileAsync(localFile, newMetadata, null,
    CancellationToken.None); // .ContinueWithOnMainThread(...

Monitor Upload Progress

You can attach listeners to uploads in order to monitor the progress of the upload. The listener follows the standard System.IProgress<T> interface. You can use an instance of the StorageProgress class, to provide your own Action<T> as a callback for progress ticks.

// Start uploading a file
var task = storageRef.Child("images/mountains.jpg")
    .PutFileAsync(localFile, null,
        new StorageProgress<UploadState>(state => {
            // called periodically during the upload
            Debug.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.");
    }
});

Error Handling

There are a number of reasons why errors may occur on upload, including the local file not existing, or the user not having permission to upload the desired file. You can find more information about errors in the Handle Errors section of the docs.

Next Steps

Now that you've uploaded files, let's learn how to download them from Cloud Storage.