You could try to use search to get the count of the files and attachments in the site collection.
Using the SharePoint search Query APIs
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.