Firebase Storage

Firebase Storage

Step 1

  1. From the navigation pane of the Firebase console , select Storage, then click Get started.
  1. Review the messaging about securing your Cloud Storage data using security rules. During development, consider setting up your rules for public access.
  1. Select a location for your default Cloud Storage bucket. • This location setting is your project's default Google Cloud Platform (GCP) resource location.
⚠️
Note that this
  • location will be used for GCP services in your project that require a location setting, specifically, your Cloud
  • Firestore database and your App Engine app (which is required if you use Cloud Scheduler). • If you aren't able to select a location, then your project already has a default GCP resource location. It was set either during project creation or when setting up another service that requires a location setting.
  • From Rules Make Sure to Remove if condition preceds by :
 

In Android

step 2

  • add Dependency
// Add the dependency for the Cloud Storage library // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-storage")
 
step 3
  • create instance of FirebaseStorage
val storage = Firebase.storage

Create a Reference

  • Create a reference to upload, download, or delete a file, or to get or update its metadata.
  • A reference can be thought of as a pointer to a file in the cloud. References are lightweight, so you can create as many as you need. They are also reusable for multiple operations.
Create a reference using the FirebaseStorage singleton instance and calling its getReference() method.
// Create a storage reference from our app var storageRef = storage.reference
Next, you can create a reference to a location lower in the tree, say "images/space.jpg" by using the child() method on an existing reference.
// Create a child reference // imagesRef now points to "images" var imagesRef: StorageReference? = storageRef.child("images") // Child references can also take paths // spaceRef now points to "images/space.jpg // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg")
 

Navigate through file herarchy

getParent() navigates up one level, while getRoot() navigates all the way to the top.
// parent allows us to move our reference to a parent node // imagesRef now points to 'images' imagesRef = spaceRef.parent // root allows us to move all the way back to the top of our bucket // rootRef now points to the root val rootRef = spaceRef.root
 

Reference Property

// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.path // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name spaceRef.name // Reference's bucket is the name of the storage bucket that the files are stored in spaceRef.bucket
 

Limitations on References

Reference paths and names can contain any sequence of valid Unicode characters, but certain restrictions are imposed including:
  1. Total length of reference.fullPath must be between 1 and 1024 bytes when UTF-8 encoded.
  1. No Carriage Return or Line Feed characters.
  1. Avoid using #, [, ], , or ?, as these do not work well with other tools such as the
 

Upload Files

To upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name.
// Create a storage reference from our app val storageRef = storage.reference // Create a reference to "mountains.jpg" val imageRef = storageRef.child("mountains.jpg") // or within folder // Create a reference to 'images/mountains.jpg' val mountainImagesRef = storageRef.child("images/mountains.jpg")
 

Upload data in memory

The putBytes() method is the simplest way to upload a file to Cloud Storage. putBytes() takes a byte[] and returns an UploadTask that you can use to manage and monitor the status of the upload.
// Get the data from an ImageView as bytes imageView.isDrawingCacheEnabled = true imageView.buildDrawingCache() val bitmap = (imageView.drawable as BitmapDrawable).bitmap val baos = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos) val data = baos.toByteArray() // this method store imge in storage var uploadTask = imageRef .putBytes(data) uploadTask.addOnFailureListener {     // Handle unsuccessful uploads }.addOnSuccessListener { taskSnapshot ->     // taskSnapshot.metadata contains file metadata such as size, content-type, etc.     // ... }
Because putBytes() accepts a byte[], it requires your app to hold the entire contents of a file in memory at once. Consider using putStream() or putFile() to use less memory.
 

Get a download URL

After uploading a file, you can get a URL to download the file by calling the getDownloadUrl() method on the StorageReference:
  • mostly this used in addOnSuccessListener method after uploading data into storage to get URL of data to used in our app
val urlTask = uploadTask.continueWithTask { task ->     if (!task.isSuccessful) {         task.exception?.let {             throw it         }     }     ref.downloadUrl }.addOnCompleteListener { task ->     if (task.isSuccessful) {         val downloadUri = task.result val imageurl: String = downloadUri.toString() Log.e("===", "submit: $imageurl")     } else {         // Handle failures         // ...     } }
 
 

Manage Uploads

In addition to starting uploads, you can pause, resume, and cancel uploads using the pause(), resume(), and cancel() methods. Pause and resume events raise pause and progress state changes respectively. Canceling an upload causes the upload to fail with an error indicating that the upload was canceled.
uploadTask = storageRef.child("images/mountains.jpg").putFile(file) // Pause the upload uploadTask.pause() // Resume the upload uploadTask.resume() // Cancel the upload uploadTask.cancel()
 
 

Example:

// Create a reference to the file in Firebase Storage val storageRef = storage.reference.child("images/${File(uri.path!!).name}") // Upload the file to Firebase Storage val uploadTask = storageRef.putBytes(imageBytes) uploadTask.addOnSuccessListener { taskSnapShot -> uploadTask.continueWithTask { task -> if (!task.isSuccessful) { task.exception?.let { throw it } } storageRef.downloadUrl }.addOnCompleteListener { task -> if (task.isSuccessful) { val downloadUri = task.result imageUrl = downloadUri.toString() Log.e("====", "submit: $imageUrl") } } }.addOnFailureListener { exception -> Log.e("====", "error: $exception") }
 
fun Uri.toBytes(contentResolver: ContentResolver): ByteArray { val inputStream: InputStream? = contentResolver.openInputStream(this) val byteBuffer = ByteArrayOutputStream() inputStream?.use { input -> val buffer = ByteArray(1024) var len: Int while (input.read(buffer).also { len = it } != -1) { byteBuffer.write(buffer, 0, len) } } return byteBuffer.toByteArray() }