ARTIFACTORY: How to find the cumulative size and total number of artifacts under a folder in an Artifactory

ARTIFACTORY: How to find the cumulative size and total number of artifacts under a folder in an Artifactory

AuthorFullName__c
Ujwal Poovaiah
articleNumber
000005519
ft:sourceType
Salesforce
FirstPublishedDate
2022-12-22T08:32:18Z
lastModifiedDate
2023-01-22T11:07:26Z
VersionNumber
2

In Artifactory UI, it is possible to get the size & count of artifacts of a folder easily, however, there is no direct REST API to get this information. We can achieve this use case in a couple of ways by combining the REST call file list endpoint as below:

→ Get the total number of artifacts under a folder (Example:hello-world):

$ curl -uadmin:password "http://localhost:8081/artifactory/api/storage/docker-local/hello-world?list&deep=1" -s | jq -c '[.files[].uri] | length'

→ Get the cumulative size (in bytes) of artifacts under this path (Example:docker-local/hello-world):
  $ curl -uadmin:password "http://localhost:8081/artifactory/api/storage/docker-local/hello-world?list&deep=1" -s | jq -c '[.files[].size] | add' | numfmt --to=iec

→ Also, we can use the below script with AQL queries to print each folder size of a repository and can extend this script according to the use case.

$ cat folderSizeAtRepositoryLevel.sh
#!/bin/bash

curl -s -k -H 'Content-Type:text/plain' -uadmin:password -XPOST "<JFROG-URL>/artifactory/api/search/aql" -d 'items.find({"type":"folder","repo":"docker-local","depth":1})' | grep "name" | awk '{print $3}' | sed 's/"//g'  | sed 's/,//g' | awk 'length > 1' > folders.txt

for folder in $(cat folders.txt);
do
echo "Folder size for -->"  $folder

curl -X POST -H "Content-Type:application/json" -uadmin:password "<JFROG-URL>//artifactory/ui/artifactgeneral/artifactsCount?$no_spinner=true" -d '{"name":"'"$folder"'","repositoryPath":"docker-local/'"$folder"'/"}'

printf "\n"
done