Reputation: 258
How can I execute the following from Java code to get the S3 bucket size
aws s3api list-objects --bucket BUCKETNAME --output json --query "[sum(Contents[].Size), length(Contents[])]"
Upvotes: 1
Views: 8926
Reputation: 165
Java's ProcessBuilder class can call the AWS CLI from Java pretty easily :
ProcessBuilder pb = new ProcessBuilder( yourCliCommandWithArgs );
Process process = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
... then consume the CLI output as input.
I would use AWS Java SDK, and the equivalent to list objects for one path would be
ListVersionsRequest listVersionsRequest = new ListVersionsRequest()
.withBucketName( yourBucketName )
.withDelimiter( pathDelimiter ).withPrefix( "your/path/" );
You might have better luck with the Cloudwatch API, it provides total bucket sizes through the CLI and SDK BucketSizeBytes . http://docs.aws.amazon.com/AmazonS3/latest/dev/cloudwatch-monitoring.html
I use http://www.insight4storage.com/ from AWS Marketplace to track my AWS S3 storage usage trends by prefix, bucket or storage class over time. Its easier than the CLI or SDK.
Upvotes: 0