I made an app that lets user stores images (I´m using S3 buckets for that), how can I prevent users to upload too many GB of photos so I don´t get charged a lot in my account?

Is there any option to prevent this? My intent when doing the app was as a showcase for my portfolio (trying to get my first job as a dev) so the link will be “out there” in public for everyone to see and I would like to have some sort of control regarding how much my users write data in the AWS services I´m using (my particular concern is S3, since photos nowdays are so heavy).


Related discussions:

Well as an aside make sure your billing alarms are set to whatever is your budget.

When we designed an app we learned the hard way when the monthly bill was €300 which was a lot for us students.

You could use DynamoDB to store the size of the bucket/keyspace per user and check that the size isn’t exceeded prior to uploading.

Lambda could be setup to read cloudwatch. I’m guessing all users are in the same bucket but under different prefixes. So if a certain prefix has more than the allowed volume of objects, the lambda reading from cloudwatch could trigger a second lambda to delete the oldest objects.

Limitations like that will need to be in the application logic.

Amazon does not set limits on how you scale your services other than account-wide service limits, which you can only increase via service ticket.

This is probably the best suggestion in terms of simplicity. I wish AWS turned on a ~$20 billing alarm by default.

Do you know how long the delay is between service usage and billing accrual/alarm? I believe in the past it was 12h+, not sure if it’s less now.

As mentioned previously, this should be handled by the application. Here’s a thought:

A user has one account. The account has a column that tracks total_user_data. When an image is uploaded/deleted by the user, add or subtract the photo size to/from the account’s total_user_data column:

account.total_user_data += image.size
account.total_user_data -= image.size

The total user data can then be shown to the user and throttled by the application. If the threshold is met, deactivate your image upload form.

If you want to run reports, do a query on all accounts and just add the total_user_data column.

Edit: If a total application target threshold is met, disable signups.

This is going to be expensive. It’s either going require n S3 buckets which you shouldn’t do, the paid metrics for each prefix, or scanning the bucket each time.

Its better to calculate the size as you ingest new images.

It is my experience that it takes 12h+ for all charges to show up in Cost Explorer, so I imagine the alarms have the same limitations.

First and foremost – how are users uploading images/objects to S3? Do they have to login? Do they upload directly to S3 from their browsers or are you using a proxy (e.g. Api Gateway, ALB) that triggers some backend code that process the file and saves it to S3?

One way I can think of from top of my head at the moment is presigned url with separate lambda function to generate presigned url with custom policy and record stored meta data such as client IpAddress and request counter in DynamoDB.

Your application can invoke or callback to alb endpoint target group attached to labmda and pass request with predefined parameters so that helper function can query DynamoDB for client IpAddress and counter for number of request made so far from that IpAddress).

Another method is to use API Gateway instead of attaching the lambda function to ALB. API Gateway can be adjusted to limit throttle as well as lambda invocation rate.

it’s pretty crude idea based on rate limiting by number of presigned url generation, but you get the gist of it.

But at the end of the day, it would be either doing all in your stack or aws resource do it for you. Regardless it would all come down to which would be most cost effective. $0.023 per GB upload to S3 or $0.0225 per hour ALB with $0.20 per million lambda requests and however-much total data-ingress payload.

Use presigned URLs which can be rate limited and have custom limits on size.

What are you trying to showcase in your portfolio with your app? Is it the uploader itself? The UX? You can set you bucket to delete objects after a certain amount of time. If it is not a production app then no need to keep them forever. And simply set limits on what you are allowed to upload.

I see lots of solutions to check if a large file was upload, but I think you’re spot on. Check before uploading, in the application.

I’d recommend setting up a lifecycle policy on the bucket to delete objects after x days if you don’t need to store the object for long term. It’ll keep your cost down and simple to implement.

Is this a product you’re trying to make available to people? Or just a proof of concept to show off work?

If it’s just a PoC why not automatically delete all data in the buckets after a day or two?

If you do this based on submitted upload size/decoupled from S3, you might get a little bit off sometimes but you’ll probably save some money by not needing to enable the deeper logging via cloudwatch etc. that will enable you to make decisions based of that. +/- a few megs per user is almost certainly cheaper than logging/infra to enable deeper S3 inspection.

The policy document signed to allow upload can limit it

Limit size? Do you have a doc or source for this. Not coming after you, I’m genuinely interested.

In the past we did the signing in our back end

https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html

Today you’d do it like all the other comments in this thread and use a lambda