Finding storage usage of Confluence space and page using REST API

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Problem

Storage usage can be tracked per product but not by individual space and page.

Reference: https://support.atlassian.com/security-and-access-policies/docs/track-storage-and-move-data-across-products/



Solution

Storage is limited for non-premium plans so Confluence admins can use the Confluence Cloud REST API to programmatically list the storage size of each attachment in Confluence spaces and pages, and save it in a .csv file.

To proceed, you should have:

  • a terminal with Python installed
  • some programming knowledge

Steps:

  1. Log in as a user with Confluence Administrator permission, and create or use an existing API token for your Atlassian Account.
  2. Copy and paste sample code below to a file. Change the values of USER, TOKEN, and BASE_URL to your info. 
    Note: Below may not work due to changes in the specifications of the REST API. Please refer to Confluence Cloud REST API for an up-to-date info.

    Python script
    # This code sample uses the 'requests' 'json' 'csv' library:
    import requests
    import json
    import csv
    
    #INSERT "USER", "TOKEN", "BASE_URL" HERE
    USER="user@example.com"
    TOKEN="XXXXXXXXXXXXXXXXX"
    BASE_URL="https://example.atlassian.net"
    
    with open('per_page.csv', 'w') as pagecsvfile, open('per_space.csv', 'w') as spacecsvfile:
        perPageWriter = csv.writer(pagecsvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        perSpaceWriter = csv.writer(spacecsvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        perPageWriter.writerow(['pageid','attachment_size_byte'])
        perSpaceWriter.writerow(['space_name','space_key','attachment_size_byte'])
    
        headers = {
           "Accept": "application/json"
        }
    
        response = requests.request(
           "GET",
           BASE_URL + "/wiki/rest/api/space",
           headers=headers,
           auth=(USER,TOKEN)
        )
    
        site_attachment_volume = 0 
            
        #Get all space keys
        space_key_results = json.loads(response.text)["results"]
        for space in space_key_results:
            space_attachment_volume = 0 
            #Get related page IDs from space keys
            response = requests.request(
                    "GET",
                    BASE_URL + "/wiki/rest/api/space/" + space["key"] + "/content",
                    headers=headers,
                    auth=(USER,TOKEN)
            )   
            print("Space Key: " + space["key"])
            page_results = json.loads(response.text)["page"]["results"]
            for page in page_results:
                page_attachment_volume = 0
                #Get attachments from each page
                print("   " + "Page ID: " + page["id"])
                response = requests.request(
                        "GET",
                        BASE_URL + "/wiki/rest/api/content/" + page["id"] + "/child/attachment",
                        headers=headers,
                        auth=(USER,TOKEN)
                )
                attachment_results = json.loads(response.text)["results"]
                for attachment in attachment_results:
                    print("      " + "Attachment Name: " + json.dumps(attachment["title"]) + ", " + json.dumps(attachment["extensions"]["fileSize"]) + " bytes")
                    page_attachment_volume += int(json.dumps(attachment["extensions"]["fileSize"]))
    
                space_attachment_volume += page_attachment_volume
                print("      -->" + "PAGE TOTAL: " + str(page_attachment_volume))
    
                #Write to CSV
                perPageWriter.writerow([page["id"],str(page_attachment_volume)])
    
            print("\n         " + "SPACE TOTAL: " + str(space_attachment_volume) + " bytes")
            print("----------")
    
            #Write to CSV
            perSpaceWriter.writerow([space["name"],space["key"],str(space_attachment_volume)])
  3. Execute the file. You may need to install additional "json" and "csv" python libraries.

    Bash command
    $ python <filename>



  4. In the same directory, files named "per_space.csv" and "per_page.csv" will be generated with the data stored inside.




Last modified on May 13, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.