Charge users for storage usage

129 Views Asked by At

I would like to report storage usage (transfers, file storage) from my firebase default storage bucket for each user, so I can charge them monthly according to usage.

For Example, a user stores 1gb of files, they get charged $0.10/mo. Half way through the billing period, the user stores another 1gb of files, and therefor should be charged $0/10/mo for half a month...Billing based on usage.

How can this be done? Is there best practice?

1

There are 1 best solutions below

0
On BEST ANSWER

Firebase doesn't have anything built in for this, nor does Cloud Storage as far as I know. But you can probably build it on top of these products by modeling your storage to allow tracking who "owns" each file, and then running a periodic process to determine the charges.

Determining who owns each file

You'll need to be able to associate each file with its owner, so that you can charge that owner for it. Since you mention using Firebase, it seems you want to allow uploading the files directly from the client app. In that case, I'd recommend storing the files for each user in a folder of their own and securing that with Firebase's security rules as shown here: https://firebase.google.com/docs/rules/basics#content-owner_only_access

Calculating the charges

You'll then want to periodically run a process on the server that checks all user folders and determines the total charge for that period. If you run this daily, you'd simply check the files in the user's folder and charge them for the storage for that day.


Note that there are (quite) some edge conditions in the above approach, such as a user storing-and-deleting files within a single days. You'll need to come up with a scheme to protect against such abuse. For this specific example that could be by not allowing a file to be deleted on the same day it was created. But in general, you'll need to think of abuse scenarios and devise protection against each of those.