Search code examples
google-drive-apiquotagoogle-drive-shared-drive

Get the number of elements in a Google Team Drive


Google Team Drives have a limit of 400 000 files, I would like to know how many elements have been uploaded to a particular Drive in order to get the percentage value that the Organization Admin can see from https://admin.google.com.

I cannot find any API endpoint for this value, is there a way to calculate this limit?

enter image description here


Solution

  • Answer:

    There is no endpoint which gives you this, so you would have to make a files: list call for each Drive and calculate this locally.

    Example:

    First, you will need to make a call to drives: list in order to get a list of all the shared Drives you are a member of:

    curl \
      'https://www.googleapis.com/drive/v3/drives
        ?fields=drives%2Fid
        &key=[YOUR_API_KEY]' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    Here, the field drives/id is used to just get a list of IDs.

    You can then loop through each ID with files: list to get a list of the files in that Drive:

    curl \
      'https://www.googleapis.com/drive/v3/files
        ?corpora=drive
        &driveId=[YOUR_DRIVE_ID]
        &includeItemsFromAllDrives=true
        &pageSize=1000
        &supportsAllDrives=true
        &key=[YOUR_API_KEY]' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    In this response, you may get a nextPageToken parameter. This means that there are more than 1000 files in the Drive, and so you will need to make subsequent requests, containing this page token, until you get a response without one:

    curl \
      'https://www.googleapis.com/drive/v3/files
        ?corpora=drive
        &driveId=[YOUR_DRIVE_ID] &includeItemsFromAllDrives=true
        &pageSize=1000
        &pageToken=[NEXT_PAGE_TOKEN]
        &supportsAllDrives=true
        &key=[YOUR_API_KEY]' \
      --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
      --header 'Accept: application/json' \
      --compressed
    

    Making sure to total the number of returned results for each request.

    With this of how many files are in each Shared Drive, you can calculate the percentage of the total files used per Drive mathematically:

    (no_of_files_in_drive / 400000) * 100
    

    Feature Request:

    You can however let Google know that this is a feature that is important for access to their APIs, and that you would like to request they implement it.

    Google's Issue Tracker is a place for developers to report issues and make feature requests for their development services, I'd urge you to make a feature request there. The best component to file this under would be the Google Drive component, with the Feature Request template.

    References: