-
-
Notifications
You must be signed in to change notification settings - Fork 5
Storage Upload Files
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.
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.
Once you have a reference, you can upload files to Cloud Storage in two ways:
- Upload from data in memory
- Upload from a URL representing a file on device
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);
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);
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);
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();
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);
}
Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.
Project setup
Analytics
Authentication
Dynamic Links
Google Sign In
Firestore
- Configuring the ANE
- Get Started
- Add and Manage Data
- Query Data
- Get Data
- Get Realtime Updates
- Perform Simple and Compound Queries
- Order and Limit Data
- Paginate Data
Messaging
One Signal
Performance
Remote Config
Storage
- Configuring the ANE
- Get Started
- Create a Reference
- Upload Files
- Download Files
- Use File Metadata
- Delete Files
Crashlytics
Vision
- Detect faces
- Scan barcodes
- Label images
- Recognize landmarks
- Natural Language
- Custom Models
External Links