title: Find position of an item in an S3 Bucket
body: <p>I am able to easily grab individual items from my S3 Bucket using Java, and get the object metadata, but there seems to be no easy way to grab the position of the object in the bucket. Here are the things that I do know:
  <p>
      * <a href="https://stackoverflow.com/questions/8027265/how-to-list-all-aws-s3-objects-in-a-bucket-using-java">There is a workaround in place to list all the objects</a>
  <p>
      * <a href="http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ObjectListing.html#getMarker--" rel="nofollow noreferrer">There is some sort of marker system in place for continuing ListObjectRequests</a> 
  <p>
  But what would be the easiest/most optimal way to figure out what my position is alphabetically from the end of the list?</p>
  
  <p>At this point, the solution seems to be fairly convoluted</p>
  
  <pre><code>public JSONObject objectPosition(String key) {
    ObjectListing listing = CLIENT.listObjects( bucketName, prefix );
    List&lt;String&gt; keys = new ArrayList&lt;String&gt;();
    List&lt;S3ObjectSummary&gt; summaries   = listing.getObjectSummaries();
  
    while (listing.isTruncated()) {
       for(S3ObjectSummary s3o : summaries) {
         keys.add(s3o.getKey());
        }
      listing = CLIENT.listNextBatchOfObjects (listing);
      summaries   = listing.getObjectSummaries();
    }
  
    JSONObject jo = new JSONObject();
    jo.put("totalSize", Integer.toString(keys.size()));
    jo.put("position", Integer.toString(key.indexOf(key)));
    return jo;
  }
  </code></pre>
  
  <p>But this means that every time my endpoint is touched, I not only have to query the s3 for all of the objects and their keys, but then I have to find the key I need to check the position of in a large list. Unfortunately local persistence between calls is not an option - so perhaps placing some sort of size file within the s3 bucket is the solution? </p>