title: How to get the sorted list from aws s3 bucket in an array
body: <p>I’m trying to get the list of an object from aws s3 bucket using boto. This list is made out of common elements of two different list. I want this list to be sorted by "last_modified" of an object by ascending order, from S3 bucket. Meaning, I want the old object (based on the date) to be first on my list. So, I am trying to prepare  list of an 5 elements like this. I want to take this list and process only those files that belong to this list and eventually delete those files and pickup the next list of 5 elements same way.</p>
  
  <p>Here is the bucket hierarchy:-</p>
  
  <pre><code>//ship-my-data/outputs/444556677788.tar.gz
  //ship-my-data/outputs/444556677788.tar.gz
  //ship-my-data/outputs/345345345353.tar.gz
  
  //ship-my-data/outputs1/ctrlFiles/ 444556677788.ctrl.tar.gz
  //ship-my-data/outputs1/ctrlFiles/ 123222333444.ctrl.tar.gz
  //ship-my-data/outputs1/ctrlFiles/ 769797977979.ctrl.tar.gz
  </code></pre>
  
  <p>I want to make a list of common elements from both the folder above i.e. from <code>outputs1</code> &amp; <code>ctrlFiles</code> folder.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>bucket = LogShip._aws_connection.get_bucket(aws_bucket_to_download) #Connecting to AWS s3 bucket
  
  bucket_list_ctrl = bucket.list(prefix='outputs/ctrlFiles/', delimiter='/') #get the bucket list for control files.
  ctrl_list = sorted(bucket_list_ctrl, key=lambda item1: item1.last_modified) # sort the list by last_modified date.
  
  bucket_list_tar = bucket.list(prefix='outputs/', delimiter='/') #get the list for tar files.
  tar_list = sorted(bucket_list_tar, key=lambda item2: item2.last_modified) #suppose to get the bucket list, but throwing an error #AttributeError: 'Prefix' object has no attribute 'last_modified'""
  
  for item_c in ctrl_list:
      ctrlName = str(item_c.name).split("/")[2].replace(".ctrl.tar.gz","") # cotrol file name: 1444447203130120001
      for item_t in bucket_list_tar:
          tarName = str(item_t.name).split("/")[1].replace(".tar.gz","") #tar file name: 1444447203130120001
      #now from above two lists I want to prepare a master list of an common elements which is pick up only 5 elements to proceed further.
      j = 5
      while j &lt;= 5:
          for elem in ctrlName:
              for elem in tarName:
                  master_list.append(elem)
                  j=j+1
              print master_list
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>['c', 't', 'r', 'l', 'F', 'i', 'l', 'e', 's', 'c', 't', 'r', 'l', 'F', 'i', 'l', 'e', 's', 'c', 't', 'r', 'l', 'F', 'i', 'l', 'e', 's', 'c', 't', 'r', 'l', 'F', 'i', 'l', 'e', 's', 'c', 't', 'r', 'l', 'F', 'i', 'l', 'e', 's', 'c', 't', 'r', 'l', 'F']
  </code></pre>
  
  <p>Expected output:</p>
  
  <pre><code>[444556677788, 123222333444]
  </code></pre>
  
  <p>Can anyone please help me understand where I'm making mistake?</p>