On my site I have the ability for users to upload avatars (which they upload to s3). They can only have one avatar at the moment, so I currently just have the filename set to their user id.
However, they may replace their avatars, which causes issues. Because the s3 is propagating to CloudFront, I need to invalidate the cache for the filename when they upload their new avatar (because it replaces the old avatar file). But with CloudFront invalidations are expensive.
It almost seems like Amazon would prefer I just not delete the old files, and instead just give the new avatar a new name and leave the old one in there, even though it's no longer being used by anyone. That way I can avoid the invalidation.
The other option is not to use CloudFront at all, and just upload to s3, and make the s3 bucket public. But the problem with this is that CloudFront is much more generous with their GET request costs and amounts (2,000,000 per month with CloudFront and only 20,000 per month with S3 buckets).
Basically, I have these 3 options, and I'm not sure which to pick, given the above:
1) Replace the files in s3 and then invalidate the file in CloudFront
- Problem: invalidations are expensive
2) Just dump more files in the s3 bucket and use CloudFront
- Problem: the old, unused avatars just sit there wasting s3 bucket space
3) Don't use CloudFront, and just replace the file in s3 and then serve it from the bucket
- Problem: CloudFront has cheaper GET requests and better user experience with latency
Edit: In this case, I'm asking about AWS with s3 and CloudFront. I'm looking to most effectively automate the process by which a user can upload an avatar to my backend service. Specifically, the overwriting of old avatars has me confused. The general process flow of "PreSigned URL -> Upload to s3 -> Propagate via CloudFront" seems an adequate one, but I'm not sure how to accommodate the overwriting of old, no longer used files in this plan, considering the choice seems to be between an expensive invalidation, or the keeping of stale files.
Are there particular communication, process or automation aspects which you are asking about?
by adding a new paragraph to the Q&A.