I wanted to know the total size of a folder stored in S3 using AWS-SDK.
Note:-
I don't want to use any command or AWS console to find the size of my folder I wanted to do this by aws-sdk and I mentioned it above so please don't mark this as duplicate.
so far what I found on the internet is to list down all the objects of folder and iterate throw it and i do this and it's working fine. here is my code :-
import AWS from 'aws-sdk';
AWS.config.region = "BUCKET_REGION";
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: "COGNITO_ID",
});
let bucketName = "BUCKET_NAME"
let bucket = new AWS.S3({
params: {
Bucket: bucketName
}
});
bucket.listObjects({Prefix:"FOLDER_NAME",Bucket:"BUCKET_NAME"}, function (err, data) {
if (err) {
console.log(err)
} else {
console.log(data)
//data returns the array throw which I iterate and find the total size of the object
}
});
but what is the problem is that there is a point of time when my folder contains so many objects that it makes it hard to iterate each one of the elements in the list. it takes to much time to just calculate the size of the folder.
so I need a better way to calculate the size of folder and all I found is this command
aws s3 ls s3://myBucket/level1/level2/ --recursive --summarize | awk 'BEGIN{ FS= " "} /Total Size/ {print $3}'
is there any way I can do the above process throw my aws-sdk.
any kind of help is appreciated. thanks in advance