Firebase Storage: Is it possible to auto - delete old files? - firebase

I use firebase cloud storage to upload images.
The app I am working on allows users to send images to one another (a chat thing), so that one user uploads the photo and another one will download it and once it is downloaded it should be deleted from the storage.
Example of what I am talking about
User A sends a photo to User B by uploading it to firebase storage, then User B notices that User A send him an image and decides to download it, after User B downloaded the image it should be deleted from storage.
My question
What if User A sends too many images and User B never downloads any of these images. Then this means that I will end with useless images on storage taking space.
So in this case is there a way in firebase to auto delete a file after it has been uploaded after (n) amount of time (not client side)?

I am still in the middle of doing this research, but it seems like you can use life cycle rules to delete files based on the age of the file.
Here are a few examples listed in the intro of the doc.
Downgrade the storage class of objects older than 365 days to
Coldline Storage.
Delete objects created before January 1, 2013.
Keep only the 3 most recent versions of each object in a bucket with
versioning enabled

Related

Get a permanent URL from Google Cloud Storage?

I have a scenario where a user can upload images to Firebase Storage, however I do not want them to be able to get a URL for these images (Copy Image Address). Instead I want to provide them with a blob.
When a user uploads the image client side, I get the download URL and store that in Firestore. When the user wants to see the image, I have a cloud function that downloads that image, and sends it to the user as a blob.
This works great for images the user uploads, however I also have a cloud function that is triggered automatically when an image is uploaded and generates a thumbnail.
How do I go about getting a permanent download URL from a Cloud Function/Node server for this generated image? I can get a signed one, but it's not what I need
You can't directly with Google Cloud Storage. The signed URL can't live more than 7 days
The longest expiration value is 604800 seconds (7days).
You can keep the link of the generated thumbnail image, but you have to either download it and serve it each time, or generate a signed url for using and displaying it, each time also. This second solution reduces the processing time and thus the cost.
If you want a permanent URL to access your data, you could make them public according to this documentation[1].
Just keep in mind that when accessing public data through the Google Cloud Platform Console, you must authenticate with Google. It can be accessed with any Google account, the account does not have to be associated with the project that contains the public data, nor must it be registered in the Cloud Storage service [2].
[1] https://cloud.google.com/storage/docs/access-control/making-data-public
[2] https://cloud.google.com/storage/docs/access-public-data
If you are using Django and django-storages[google] lib
Make the bucket public(not advisable for production)
Disable ACL and URL signing
Add these to the settings.py
GS_DEFAULT_ACL = None
GS_QUERYSTRING_AUTH = False

Is the Firebase Console "Data View" always downloading my full realtime database?

Let's say we have a database of 10 GB with a structure like that (with more books, etc):
What happens if I open this database in the Data View of the Firebase console?
I've learned that with the realtime database it's not possible to just read the keys of an object.
How does the Data View know the subkeys of the root (such as "channels", "input") without downloading all the contained data. Further more:
Do I have to wait until "channels" is completely downloaded to see "inbox"?
Did I then cause 10 GB download costs? (assumed I wait accordingly)
If your database is large the Firebase console switches to "read-only" mode. In that mode the console uses the REST API to download a shallow listing of only the keys of the level you're seeing. This should drastically reduce the amount of data it downloads.

Google in app purchases google play store on firebase : )

I am a young female developer, but currently i am building a project for a big client in our company,
I have a question, actually one of you can probablly lead me in the right direction:
I have a firebase account and an app with in app purchases - the thing is in app purchases will actually contain videos which i would like to store on firebase servers and download uppon purchase! Can someone lead me in the right direction on how to implement this the easiest way?
Ps., I know i am a rookie but i really do not want to look like an idiot on next months meeting ; )
Warm regards, Lana
Theres honestly no easy way to achieve protecting a file/folder for a specific group of users with firebase cloud storage. You can do checks by changing the folders metadata or using custom tokens, but then you need a server for that etc...
If you wish to remain in a firebase only environment, the easiest way would probably be to have the protected videos hosted in a protected folder from all clients. When the user purchases a video, then send a request a CloudFunction along the lines of..
userClaimsToHaveBoughtAccessToVideo(userId, sku, token, videoId){
// 1. use the googlePlay billing API to verify that the user did indeed purchase the item
// 2. if they did, then store that in read-only node of the database like: /purchasedVideos/userId/videoID/
}
Then if a user wants to download a video, create a function along the lines of...
userWantsToDownloadAVideo(userId, videoId){
// check the database to see if the user has the video
// if they do, copy the video file to a folder like /userId/videos/...
}
Then the video will appear in a personal folder for which the security rules are easy to setup.
When the user downloaded the video to their device send a call to a function along the lines of..
userDownloadedVideo(userId, videoId){
// delete video from the user's folder, to save space
}
Another option would be to host the videos on a different platform entirely, one that has an API and allows it's files to be password protected. (then simply store the access password after verifying IAP in the database, read only for the user), so they can use the other API to download the videos. For security you might want to have a daily job which regenerates the password every day and updates the database..

Firebase Storage – getting static link based on filename

I have a situation where in Firebase Storage users store their avatar to /users/{uid}.jpg
If I then use the Storage API to get the download URL and display the image, it ends up being very slow to make the first request because the download URL is not cached anywhere.
So my solution is to get the DownloadURL when the user uploads the image and store that in Firebase allowing the client image provider to automatically cache the image which speeds up loads considerably.
However, there is one problem with this solution.
If a user replaces their avatar, the old link becomes broken instead of updated. I believe this is because a new token is generated each time something is uploaded for security reasons but these are of no benefit to me.
So my question is twofold:
1) How can I allow a user to upload an avatar to a path that is dedicated to them such as /users/{uid}.jpg, get a bare download URL that can be cached by the client, and have that URL remain the same even when the file changes at /users/{uid}.jpg
2) If this is not possible, what is the normal way to solve this issue?
Download URLs are opaque. The contents of the actual URL itself is an implementation detail of the system, and it's not supported to dig around in its contents. The URLs can't be dissected or composed.
You can use a storage trigger with Cloud Functions to automatically generate a signed URL whenever something changes in your storage bucket.
So instead of serving from a hard-coded URL, simply retrieve the URL from an updated value in the datastore (or any data storage system). Every time the user updates the avatar, simply store the new URL in the datastore and you can query for it when you need it.

Adding Firebase Storage user upload limits

Is there any way to add Firebase 3 Storage security rules to limit how many files can single authenticated user upload? For example 100 files per user.
Or somehow update Firebase Database file count, once someone uploaded file to Storage and later validate that file count.
Trying to solve problem, how to deal with user ability to upload unlimited data amount to storage.
It's not a simple solution, but...
https://medium.com/#felipepastoree/per-user-storage-limit-validation-with-firebase-19ab3341492d

Resources