Upload Files on iOS

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

Create a Reference

To upload a file, first create a Firebase Storage reference to the location in Firebase Storage you want to upload the file to.

You can create a reference by appending child paths to the storage root:

Objective-C

    // Create a root reference
    FIRStorageReference *storageRef = [storage reference];

    // Create a reference to "mountains.jpg"
    FIRStorageReference *mountainsRef = [storageRef child:@"mountains.jpg"];

    // Create a reference to 'images/mountains.jpg'
    FIRStorageReference *mountainImagesRef = [storageRef child:@"images/mountains.jpg"];

    // While the file names are the same, the references point to different files
    mountainsRef.name == mountainImagesRef.name            // true
    mountainsRef.fullPath == mountainImagesRef.fullPath    // false
  

Swift

    // Create a root reference
    let storageRef = storage.reference()

    // Create a reference to "mountains.jpg"
    let mountainsRef = storageRef.child("mountains.jpg")

    // Create a reference to 'images/mountains.jpg'
    let mountainImagesRef = storageRef.child("images/mountains.jpg")

    // While the file names are the same, the references point to different files
    mountainsRef.name == mountainImagesRef.name            // true
    mountainsRef.fullPath == mountainImagesRef.fullPath    // false
    

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

Upload Files

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

  1. Upload from NSData in memory
  2. Upload from an NSURL representing a file on device

Upload from data in memory

The putData:metadata:completion: method is the simplest way to upload a file to Firebase Storage. putData:metadata:completion: takes an NSData object and returns an FIRStorageUploadTask, which you can use to manage your upload and monitor its status.

Objective-C

    // Data in memory
    NSData *data = ...

    // Create a reference to the file you want to upload
    FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

    // Upload the file to the path "images/rivers.jpg"
    FIRStorageUploadTask *uploadTask = [riversRef putData:data metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        NSURL *downloadURL = metadata.downloadURL;
      }
    }];
  

Swift

    // Data in memory
    let data: NSData = ...

    // Create a reference to the file you want to upload
    let riversRef = storageRef.child("images/rivers.jpg")

    // Upload the file to the path "images/rivers.jpg"
    let uploadTask = riversRef.putData(data, metadata: nil) { metadata, error in
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        let downloadURL = metadata!.downloadURL
      }
    }
    

Upload from a local file

You can upload local files on the devices, such as photos and videos from the camera, with the putFile:metadata:completion: method. putFile:metadata:completion: takes an NSURL and returns an FIRStorageUploadTask, which you can use to manage your upload and monitor its status.

Objective-C

    // File located on disk
    NSURL *localFile = ...

    // Create a reference to the file you want to upload
    FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

    // Upload the file to the path "images/rivers.jpg"
    FIRStorageUploadTask *uploadTask = [riversRef putFile:localFile metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        NSURL *downloadURL = metadata.downloadURL;
      }
    }];
  

Swift

    // File located on disk
    let localFile: NSURL = ...

    // Create a reference to the file you want to upload
    let riversRef = storageRef.child("images/rivers.jpg")

    // Upload the file to the path "images/rivers.jpg"
    let uploadTask = riversRef.putFile(localFile, metadata: nil) { metadata, error in
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        // Metadata contains file metadata such as size, content-type, and download URL.
        let downloadURL = metadata!.downloadURL
      }
    }
    

If you want to actively manage your upload, you can use the putData: or putFile: methods and observe the upload task, rather than using the completion handler. 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 putFile: method automatically infers the content type from the NSURL filename extension, but you can override the auto-detected type by specifying contentType in the metadata. If you do not provide a contentType and Firebase Storage cannot infer a default from the file extension, Firebase Storage uses application/octet-stream. See the Use File Metadata section for more information about file metadata.

Objective-C

    // Create storage reference
    FIRStorageReference *mountainsRef = [storageRef child:@"images/mountains.jpg"];

    // Create file metadata including the content type
    FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
    metadata.contentType = @"image/jpeg";

    // Upload data and metadata
    FIRStorageUploadTask *uploadTask = [mountainsRef putData:data metadata:metadata];

    // Upload file and metadata
    FIRStorageUploadTask *uploadTask = [mountainsRef putFile:localFile metadata:metadata];
  

Swift

    // Create storage reference
    let mountainsRef = storageRef.child("images/mountains.jpg")

    // Create file metadata including the content type
    var metadata = FIRStorageMetadata()
    metadata.contentType = "image/jpeg"

    // Upload data and metadata
    let uploadTask = mountainsRef.putData(data, metadata: metadata)

    // Upload file and metadata
    let uploadTask = mountainsRef.putFile(localFile, metadata: metadata)
    

Manage Uploads

In addition to starting uploads, you can pause, resume, and cancel uploads using the pause, resume, and cancel methods. These methods raise pause, resume, and cancel events. Canceling an upload causes the upload to fail with an error indicating that the upload was canceled.

Objective-C

    // Start uploading a file
    FIRStorageUploadTask *uploadTask = [mountainsRef putFile:localFile];

    // Pause the upload
    [uploadTask pause];

    // Resume the upload
    [uploadTask resume];

    // Cancel the upload
    [uploadTask cancel];
  

Swift

    // Start uploading a file
    let uploadTask = mountainsRef.putFile(localFile)

    // Pause the upload
    uploadTask.pause()

    // Resume the upload
    uploadTask.resume()

    // Cancel the upload
    uploadTask.cancel()
    

Monitor Upload Progress

You can attach observers to FIRStorageUploadTasks in order to monitor the progress of the upload. Adding an observer returns a FIRStorageHandle that can be used to remove the observer.

Objective-C

    // Add a progress observer to an upload task
    FIRStorageHandle observer = [uploadTask observeStatus:FIRStorageTaskStatusProgress
                                                  handler:^(FIRStorageTaskSnapshot *snapshot) {
      // A progress event occurred
    }];
  

Swift

    // Add a progress observer to an upload task
    let observer = uploadTask.observeStatus(.Progress) { snapshot in
      // A progress event occurred
    }
    

These observers can be added to an FIRStorageTaskStatus event:

FIRStorageTaskStatus Event Typical Usage
FIRStorageTaskStatusResume This event fires when the task starts or resumes uploading, and is often used in conjunction with the FIRStorageTaskStatusPause event.
FIRStorageTaskStatusProgress This event fires any time data is uploading to Firebase Storage, and can be used to populate an upload progress indicator.
FIRStorageTaskStatusPause This event fires any time the upload is paused, and is often used in conjunction with the FIRStorageTaskStatusResume event.
FIRStorageTaskStatusSuccess This event fires when a upload has completed sucessfully.
FIRStorageTaskStatusFailure This event fires when a upload has failed. Inspect the error to determine the failure reason.

When an event occurs, an FIRStorageTaskSnapshot object is passed back. This snapshot is an immutable view of the task, at the time the event occurred. This object contains the following properties:

Property Type Description
progress NSProgress An NSProgress object containing the progress of the upload.
error NSError An error that occurred during upload, if any.
metadata FIRStorageMetadata During upload contains metadata being uploaded. After an FIRTaskStatusSuccess event, contains the uploaded file's metadata.
task FIRStorageUploadTask The task this is a snapshot of, which can be used to manage (pause, resume, cancel) the task.
reference FIRStorageReference The reference this task came from.

You can also remove observers, either individually, by status, or by removing all of them.

Objective-C

    // Create a task listener handle
    FIRStorageHandle observer = ...

    // Remove an individual observer
    [uploadTask removeObserverWithHandle:observer];

    // Remove all observers of a particular status
    [uploadTask removeAllObserversForStatus:FIRStorageTaskStatusProgress];

    // Remove all observers
    [uploadTask removeAllObservers];
  

Swift

    // Create a task listener handle
    let observer = ...

    // Remove an individual observer
    uploadTask.removeObserverWithHandle(observer)

    // Remove all observers of a particular status
    uploadTask.removeAllObserversForStatus(.Progress)

    // Remove all observers
    uploadTask.removeAllObservers()
    

To prevent memory leaks, all observers are removed after an FIRStorageTaskStatusSuccess or FIRStorageTaskStatusFailure occurs.

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.

Full Example

A full example of an upload with progress monitoring and error handling is shown below:

Objective-C

    // Local file you want to upload
    NSURL *localFile = ...

    // Create the file metadata
    FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
    metadata.contentType = @"image/jpeg";

    // Upload file and metadata to the object 'images/mountains.jpg'
    FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile metadata:metadata];

    // Listen for state changes, errors, and completion of the upload.
    [uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
      // Upload resumed, also fires when the upload starts
    }];

    [uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
      // Upload paused
    }];

    [uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
      // Upload reported progress
      double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
    }];

    [uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
      // Upload completed successfully
    }];

    // Errors only occur in the "Failure" case
    [uploadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
      if (snapshot.error != nil) {
        switch (snapshot.error.code) {
          case FIRStorageErrorCodeObjectNotFound:
            // File doesn't exist
            break;

          case FIRStorageErrorCodeUnauthorized:
            // User doesn't have permission to access file
            break;

          case FIRStorageErrorCodeCancelled:
            // User canceled the upload
            break;

          ...

          case FIRStorageErrorCodeUnknown:
            // Unknown error occurred, inspect the server response
            break;
        }
      }
    }];
  

Swift

    // Local file you want to upload
    let localFile: NSURL = ...

    // Create the file metadata
    let metadata = FIRStorageMetadata()
    metadata.contentType = "image/jpeg"

    // Upload file and metadata to the object 'images/mountains.jpg'
    let uploadTask = storageRef.child("images/mountains.jpg").putFile(localFile, metadata: metadata);

    // Listen for state changes, errors, and completion of the upload.
    uploadTask.observeStatus(.Pause) { snapshot in
      // Upload paused
    }

    uploadTask.observeStatus(.Resume) { snapshot in
      // Upload resumed, also fires when the upload starts
    }

    uploadTask.observeStatus(.Progress) { snapshot in
      // Upload reported progress
      if let progress = snapshot.progress {
        let percentComplete = 100.0 * Double(progress.completedUnitCount) / Double(progress.totalUnitCount)
      }
    }

    uploadTask.observeStatus(.Success) { snapshot in
      // Upload completed successfully
    }

    // Errors only occur in the "Failure" case
    uploadTask.observeStatus(.Failure) { snapshot in
      guard let storageError = snapshot.error else { return }
      guard let errorCode = FIRStorageErrorCode(rawValue: storageError.code) else { return }
      switch errorCode {
        case .ObjectNotFound:
          // File doesn't exist

        case .Unauthorized:
          // User doesn't have permission to access file

        case .Cancelled:
          // User canceled the upload

        ...

        case .Unknown:
          // Unknown error occurred, inspect the server response
      }
    }
    

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

Send feedback about...

Need help? Visit our support page.