Skip to content

Storage Upload Files

Eoin Landy edited this page Jan 30, 2020 · 3 revisions

The contents of this page are based on the original Firebase Documentation

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

Note: By default, Cloud Storage buckets require Firebase Authentication to upload files. You can change your Firebase Security Rules for Cloud Storage to allow unauthenticated access. Since the default Google App Engine app and Firebase share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible as well. Be sure to restrict access to your Storage bucket again when you set up authentication.

Create a Reference

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

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

// Create a root reference
var storageRef:StorageReference = storage.reference();

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

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

// While the file names are the same, the references point to different files
mountainsRef.name == mountainImagesRef.name;            // true
mountainsRef.path == mountainImagesRef.path;    // 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 Cloud Storage in two ways:

  1. Upload from data in memory
  2. Upload from a URL representing a file on device

Upload from data in memory

var data:ByteArray = new ByteArray();
// Create a reference to the file you want to upload
var riversRef:StorageReference = storageRef.child("images/rivers.jpg");

var uploadTask:UploadTask = storageRef.putData(data);
uploadTask.addEventListener(StorageEvent.TASK_COMPLETE, onUploadBytesSuccess, false, 0, true);

Upload 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 File and returns an UploadTask, which you can use to manage your upload and monitor its status.

var localFile:File = File.applicationStorageDirectory.resolvePath("local-logo.png");
// Create a reference to the file you want to upload
var riversRef:StorageReference = storageRef.child("images/rivers.jpg");

var uploadTask:UploadTask = storageRef.putFile(imageFile);
uploadTask.addEventListener(StorageEvent.TASK_COMPLETE, onUploadFileSuccess, false, 0, true);

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 File 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
var mountainsRef:StorageReference = storageRef.child("images/mountains.jpg");

// Create file metadata including the content type
var metadata:StorageMetadata = new StorageMetadata();
metadata.contentType = "image/jpeg";

// Upload file and metadata
mountainsRef.putFile(localFile, metadata);

Manage Uploads

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

// Start uploading a file
var uploadTask:UploadTask = storageRef.putFile(imageFile)

// Pause the upload
uploadTask.pause();

// Resume the upload
uploadTask.resume();

// Cancel the upload
uploadTask.cancel();

Monitor Upload Progress

You can attach listeners to UploadTasks in order to monitor the progress of the upload.

uploadTask.addEventListener(StorageProgressEvent.PROGRESS, onUploadProgress);
private function onUploadProgress(event:StorageProgressEvent):void {
    trace(event);
}
Clone this wiki locally