Storage Download Files - tuarua/Firebase-ANE GitHub Wiki

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

Cloud Storage allows developers to quickly and easily download 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 download a file, first create a Cloud Storage reference to the file you want to download. You can create a reference by appending child paths to the storage root, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.

// Create a reference with an initial file path and name
var pathReference:StorageReference = storage.reference("images/stars.jpg");

// Create a reference from a Google Cloud Storage URI
var gsReference:StorageReference = storage.reference(null, "gs://<your-firebase-storage-bucket>/images/stars.jpg");

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
var httpsReference:StorageReference = storage.reference(null, "https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");

Download in memory

Download the file to a ByteArray in memory using the getData 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.

var islandRef:StorageReference = storageRef.child("images/island.jpg")
var downloadTask:DownloadTask = islandRef.getData(1 * 1024 * 1024);
downloadTask.addEventListener(StorageEvent.TASK_COMPLETE, onDownloadBytesSuccess, false, 0, true);
downloadTask.addEventListener(StorageErrorEvent.ERROR, onDownloadError, false, 0, true)

Download to a local file

The write 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. write returns an DownloadTask which you can use to manage your download and monitor the status of the upload.

var islandRef:StorageReference = storageRef.child("images/island.jpg")
var downloadTask:DownloadTask = islandRef.write(File.applicationStorageDirectory.resolvePath("downloaded-logo.png"));
downloadTask.addEventListener(StorageEvent.TASK_COMPLETE, onDownloadFileSuccess, false, 0, true);
downloadTask.addEventListener(StorageErrorEvent.ERROR, onDownloadError, false, 0, true);

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 downloadUrl method on a storage reference.

// Create a reference to the file you want to download
var starsRef:StorageReference = storageRef.child("images/stars.jpg");
starsRef.downloadUrl(function (url:String, error:StorageError):void {
    trace(url);
});

Manage Downloads

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

// Start downloading a file
var downloadTask:DownloadTask = islandRef.write(File.applicationStorageDirectory.resolvePath("downloaded-logo.png"));

// Pause the download
downloadTask.pause();

// Resume the download
downloadTask.resume();

// Cancel the download
downloadTask.cancel();

Monitor Download Progress

You can attach listeners to DownloadTasks in order to monitor the progress of the download.

downloadTask.addEventListener(StorageProgressEvent.PROGRESS, onDownloadProgress);
private function onDownloadProgress(event:StorageProgressEvent):void {
    trace(event);
}