API : Get Bucket size and number of class A B operations

Hi !

I’d like to get the current size of an R2 bucket and how many class A and class B have been performed through the API, for reporting purposes.
I only found how to list buckets, but I can’t see how to get metrics data.
The info is available in the console though, so it should lay somewhere :slight_smile:

would anyone know how to achieve this ?

Thanks

I found how to do it !

here’s a sample python code :

import requests

accoundID = 'xxxxxxxxxx'
bucket_name = 'xxxxxx'
api_token = 'xxxxxxxxxx'

url = f"https://api.cloudflare.com/client/v4/accounts/{accoundID}/r2/buckets/{bucket_name}/usage"

headers = {
    "Authorization": f"Bearer {api_token}",
    "Content-Type": "application/json",
}

response = requests.get(url, headers=headers)

json_data = response.json()
result = json_data['result']

payload_size = result['payloadSize']
metadata_size = result['metadataSize']
object_count = result['objectCount']

I’m looking for the same information. Your answer doesn’t provide information about metrics of Class A & B operations, but the overall usage of storage of the bucket.

@Cloudflare team: Is there an API, or another way, we can read metrics programatically for R2 buckets, ideally with parameters such as report interval, aggregation period & function.

My team wants to use this service as alternative to a different CDN from a public cloud provider, and visibility is a requirement to properly test Cloudflare’s solution, which looks very promising on paper.

There seems to be a website analytics page at
https://dash.cloudflare.com/<account_id>/<website_domain>/analytics/traffic
that will show traffic stats, but seems this is at domain level and don’t see drill-in options to look at analytics per subdomain or configure what data are you looking at (average, sums, aggregation period etc)

1 Like

How are the api tokens used here generated? I didn’t see the API token corresponding to R2 when I created it. Can you tell me how to generate this API token?

Those are Cloudflare API tokens, not R2 specific. You create an API token in your user’s profile settings.