title: Calculate S3 object(folder) size in java
body: <p>Im storing all types of files on amazon S3. In amazon S3 bucket, All files store in different folders, I know there is no concept of folder in amazon s3. Objects are only identified by their keys. if i store any file with key like 'mydocs/personal/profile-pic.jpg' that mean two parents folders(personal folder inside mydocs folder) will be created there. </p>
  
  <p>I want to calculate the size of any folder like 'mydocs' in java. I calculated bucket total size by using this code given below: </p>
  
  <pre><code>public long calculateBucketSize(String bucketName) {
  long totalSize = 0;
      int totalItems = 0;
      ObjectListing objects = listObjects(bucketName);
      do {
          for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
              totalSize += objectSummary.getSize();
              totalItems++;
          }
          objects = listNextBatchOfObjects(objects);
      } while (objects.isTruncated());
      System.out.println("Amazon S3 bucket: " + bucketName + " containing "
              + totalItems + " objects with a total size of " + totalSize
              + " bytes.");
  
      return totalSize;
  }
  </code></pre>
  
  <p>This method will return the bucket total size. I want to calculate the size of any single folder.  Any help will be appreciated. </p>