-
Notifications
You must be signed in to change notification settings - Fork 16
E.4 Download Files
Firebase Storage allows developers to quickly and easily download files from a Google Cloud Storage bucket provided and managed by Firebase.
Note: By default, Firebase Storage buckets require Firebase Authentication to upload files. You can change your Firebase Storage Security Rules 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.
To download a file, first create a Firebase 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 Firebase Storage.
Once you have a reference, you can download files from Firebase Storage in three ways:
- Download to
ByteArray
in memory. - Download to a
File
representing a file on device. - Generate a URL representing the file online.
Download the file to a ByteArray
with the getBytes() method. This is the easiest way to download a file, but it must load the 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, getBytes()
takes a maximum amount of bytes to download. Set the maximum size to something you know your app can handle, or use another download method.
var rootRef:StorageReference = Storage.getReference();
var imgRef:StorageReference = rootRef.child("images/img.png");
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
imgRef.getBytes(1024*1024, onGetBytesSuccess, onGetBytesFailed);
function onGetBytesFailed(e:StorageEvents):void
{
trace("onGetBytesFailed, errorCode = " + e.errorCode + " with message: " + e.msg);
}
function onGetBytesSuccess(e:StorageEvents):void
{
var loader:Loader = new Loader();
loader.loadBytes(e.bytes);
addChild(loader);
}
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 the file in a different app. getFile()
returns a FileDownloadTask which you can use to manage your download and monitor the status of the download.
var rootRef:StorageReference = Storage.getReference();
var imgRef:StorageReference = rootRef.child("images/img.png");
// deside where you want to save the downloaded file. Note that this location must be writable.
var distination:File = File.documentsDirectory.resolvePath("img.png");
var fileDownloadTask:FileDownloadTask = imgRef.getFile(distination);
// monitor the download progress with the following listeners
fileDownloadTask.addEventListener(StorageEvents.TASK_FAILED, onDownloadFileFailed);
fileDownloadTask.addEventListener(StorageEvents.TASK_PROGRESS, onDownloadFileProgress);
fileDownloadTask.addEventListener(StorageEvents.TASK_SUCCESS, onDownloadFileSuccess);
function onDownloadFileFailed(e:StorageEvents):void
{
// make sure to remove the listeners when they are no longer needed
trace("onDownloadFileFailed with errorCode '" + e.errorCode + "' and msg: " + e.msg);
}
function onDownloadFileProgress(e:StorageEvents):void
{
var percent:Number = fileDownloadTask.bytesTransferred / fileDownloadTask.bytesTotal * 100;
trace("onDownloadFileProgress = " + Math.floor(percent) + "%");
}
function onDownloadFileSuccess(e:StorageEvents):void
{
// make sure to remove the listeners when they are no longer needed
trace("onDownloadFileSuccess");
}
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 storage reference.
imgRef.getDownloadUrl(onGetDownloadUrlSuccess, onGetDownloadUrlFailed);
function onGetDownloadUrlFailed(e:StorageEvents):void
{
trace("onGetDownloadUrlFailed with errorCode '" + e.errorCode + "' and msg: " + e.msg);
}
function onGetDownloadUrlSuccess(e:StorageEvents):void
{
trace("onGetDownloadUrlSuccess url = " + e.url);
}
Enjoy building Air apps – With ♥ from MyFlashLabs Team
Introduction to Firebase ANEs collection for Adobe Air apps
Get Started with Firebase Core in AIR
- Prerequisites
- Add Firebase to your app
- Add the Firebase SDK
- Init Firebase Core
- Available ANEs
- Managing Firebase iid
Get Started with Authentication
- Add Authentication
- Init Authentication
- Manage Users
- Phone Number
- Custom Auth
- Anonymous Auth
- State in Email Actions
- Email Link Authentication
Get Started with FCM + OneSignal
- Add FCM ANE
- Init FCM ANE
- Send Your 1st Message
- Send Msg to Topics
- Understanding FCM Messages
- init OneSignal
- Add Firestore
- Init Firestore
- Add Data
- Transactions & Batches
- Delete Data
- Manage the Console
- Get Data
- Get Realtime Updates
- Simple and Compound
- Order and Limit Data
- Paginate Data
- Manage Indexes
- Secure Data
- Offline Data
- Where to Go From Here
Get Started with Realtime Database
- Add Realtime Database
- Init Realtime Database
- Structure Your Database
- Save Data
- Retrieve Data
- Enable Offline Capabilities
Get Started with Remote Config
- Add Storage ANE
- Init Storage ANE
- Upload Files to Storage
- Download Files to Air
- Use File Metadata
- Delete Files