Secure file storage with Firebase Storage

  • Implementation of a scalable storage system based on the Google Cloud Storage infrastructure.
  • Security management through a declarative language that links file access with user authentication.
  • Optimized user experience thanks to the ability to resume uploads and downloads interrupted by network failures.

Secure file storage with Firebase Storage

If you're embarking on the development of a mobile or web application, you've probably realized that managing large files like photos, videos, or documents can be a real headache. To avoid the hassle of setting up a server from scratch, Firebase Storage It is presented as the ultimate tool, allowing you to easily save user-generated content without racking your brain over the infrastructure.

The best part is that it's not just a place to drop files; it comes with a complete ecosystem. Being integrated with Firebase AuthenticationYou can decide exactly who can see what, preventing anyone from snooping into your users' private data. It's essentially Google's way of letting developers focus on app functionality instead of battling storage buckets.

What exactly is Firebase Storage and how does it work?

Essentially, it's an object storage service that leverages the power of Google Cloud StorageThis means your app can scale from a small prototype to support millions of users without breaking a sweat, as it uses the same technology that powers giants like Spotify or Google Photos. The files are stored in what we call bucketswhich are accessible from both the Firebase SDKs and the Google Cloud APIs.

One of the jewels of this service are the robust operationsWho hasn't felt the frustration of a 90% upload being cut short because of a Wi-Fi failure? Firebase solves this by allowing uploads and downloads to continue. they restart right where they left offsaving the end user time and data. Furthermore, it offers a global redundancyreplicating the files in different data centers so that access is much faster depending on where the person is located.

The pillar of safety: Storage Rules

This is where many developers get lost, but it's the most critical part. Since client-side code (JavaScript, Swift, or Kotlin) is visible and can be manipulated, you can't rely solely on the front-end for security. That's where security measures come in. Security Rules, a declarative language that runs on the server to validate each request.

The rules don't cascade; that is, granting permission to a folder doesn't automatically make all its subfolders accessible. To define access, we use the statement match to specify the file path and the statement allow to authorize reading or writing. If there is no rule that matches the requested path, Firebase, by default, will deny the action For security.

Route types and wildcards

  • Specific routes: When you want to control a single file, like /configuracion.json.
  • Wildcards at one level: Using the syntax {variable}You can refer to any file within a folder, but not to subdirectories. For example, /fotos/{imagen}.
  • Multiple wild cards: Expression {allPaths=**} It is the most powerful, as it affects all nesting levels of directories.

For the system to be truly secure, ideally access should be linked to the User UIDIn this way, you can create a structure where each person only has access to their own folder: match /usuarios/{userId}/{allPaths=**} { allow read, write: if request.auth.uid == userId; }Thus, the system checks that the unique identifier the authenticated user's name matches the name of the folder they are trying to access.

Advanced data validation

It's not enough to know who comes in; you also have to control what comes in. Through the object request.resourceWe can impose strict limits. For example, if you want to prevent someone from uploading a 1 GB file disguised as a profile picture, you can validate that the size is less than 1MB and that contentType match an image format (such image/jpeg o image/png).

The object resourceOn the other hand, it gives us information about the file that already exists on the server, allowing us to perform checks before deleting or modifying it. This combination of authentication and metadata validation makes storage secure. tobacconist and professional.

Technical implementation and workflow

To get this up and running, you must first enable the service in the console of FirebaseIf you're working with Android, you'll need to add the dependency for firebase-storage in your Gradle file. In Flutter, the process is similar, using specific packages and creating an instance with FirebaseStorage.instance.

The basic flow consists of creating a route reference (a pointer to the location where the file will go) and then execute the upload action using methods such as putFileIf you need to recover the file, the method getDownloadURL It will provide you with a temporary or permanent public link that you can use in your interface to display the image or document.

Costs and limitations of the service

Firebase offers a free plan (Spark) very generous, including up to 5 GB of storage and daily download limits of 1 GB. If your app takes off and you exceed these limits, you move to the next model. payment for usewhere a small fraction of a dollar is charged for each gigabyte per month and for the read and write operations performed.

This entire architecture allows any developer to manage the archive infrastructure efficiently, delegating network complexity and security to Google servers, while maintaining full control over data privacy through custom rules and seamless integration with authentication systems.


Add as preferred source