Count how many times a r2 file has been viewed/downloaded with GET req's

Hi! I'm currently working on a a feature that allows user to enable auto-deletion of files if the image has been viewed less than x amount of times over a certain period. All I really want to do in my worker is to listen to a GET req from any file in my bucket and and simply increment a counter in my database with the image url/id. Then I'll spin up a cron job on Vercel which handles the deletion. I have currently only made a binding between my worker and my r2 bucket, and I tried a simple test with just redirecting any requests back to my homepage, but when I try to access a file in my bucket, it shows the image, instead of redirecting, and nothing is logged either. Am I thinking about this the wrong way? Are these requests only on the worker url, and in that case, should I move my entire uploading logic to the worker, instead of having it running on a separate Go server?
1 Reply
chip
chip4mo ago
Would another option be to use the metrics/analytics api? Ok, actually, this might work Create one GET check, and replace the url for the return url when uploading an image with the url for the worker with the image key, and when we get a request to our worker, we simply get the the object from the bucket, make a count and return the body.