I can't find how to get usage metrics to my R2 bucket through Cloudflare's API.
I'd like to get the bucket size and the number of class A and class B operations.
I can't find how to get usage metrics to my R2 bucket through Cloudflare's API.
I'd like to get the bucket size and the number of class A and class B operations.
Copyright © 2021 Jogjafile Inc.
You can use their
https://api.cloudflare.com/client/v4/graphql
endpoint for both of those queries. Which is what their frontend does anyways. I'm writing this innodejs
but I did see that this can be done with cURL instead if needed.I mainly followed their docs.
Here is a JavaScript file which does both requests. If you wanted to filter to a certain bucket you would add the bucketName prop to the filter. However, keep in mind operation requests for billing purposes are account wide not per bucket.
this assumes you have a .env or .dev.vars file with these three values filled out
Generate a token.
Your account ID can be found on the right.
Then assuming you are running Node v20.6+ (which can read .env files) it will just be
node --env-file .env main.js
.Worker email
I wanted to automate this into a CloudFlare Worker. I found that there is good integration with MailChannel so that emails can be sent from an authenticated domain. There is a lot of steps to doing that and veers off the topic. So, I'll just link that here but here is the intro paragraph: