2014-06-27 50 views
0

,我将它们存储在为格式对象:AWS S3博托检索日期

网站/网页/ objecthash/xcord/ycord /情形产生/年/月/ datenumber /小时/分钟

现在我有桶我想把他们拉出来。

说我想要最近10个存储的对象。什么是有效的方法来做到这一点?

我有桶,我该怎么办?

我的解决办法是这样的拿到今天然而,我不知道该逻辑获取最新:

def getKeys(): 
    b = bucket.list() 
    theKeys=[] 
    for key in b: 
     theKeys.append(key) 
    return theKeys 

def getDecompiledToday(): 
    time = datetime.datetime.now()  
    year =time.strftime("%Y") 
    month = time.strftime("%m") 
    day = time.strftime("%D") 
    keys = getKeys() 
    objects = [] 
    for k in keys: 
     splitK= k.split("/") 
     if splitK[6]==year and splitK[7]==month and splitK[8]==day: 
      objets.append(bucket.get_key(k)) 
    return 
+0

我觉得这个基本上是一样的问题:http://stackoverflow.com/questions/24282214/python-boto-for-aws-s3-how-to-get-sorted-and-limited- files-list-in-bucket/24290121#24290121 – garnaat

+0

我相信这是不同的,因为我已经存储了时间/日期。我不是在寻找一种原生的s3方法。此外,我将日期以后缀方式存储,而不是前缀。 – Tai

+0

编辑我的问题,使其更明显,除非有另一种解决方案更容易,我最大的问题是正确获得最新的有效方式背后的逻辑。谢谢 – Tai

回答

0

,我想出了一个解决方案。

def getPastAmountDecompiledFromFile(number): 
    if bucketKeys.__len__() > 0: 
     Found=[] 
     latest=bucketKeys[0] 
     while Found.__len__() < number: 
      laterFound = False 
      for k in bucketKeys: 
       if latest in Found: 
        latest=k 
       current = k.split("/") 
       best = k.split("/") 
       if k not in Found and latest != k: 
        if int(current[6]) > int(best[6]): 
         laterFound=True 
        if int(current[6]) == int(best[6]) and int(current[7]) > int(best[7]): 
         laterFound=True 
        if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) > int(best[8]): 
         laterFound=True 
        if int(current[6]) == int(best[6]) and int(current[7]) == int(best[7]) and int(current[8]) == int(best[8]) and int(current[9]) > int(best[9]): 
         laterFound=True 
        if laterFound: 
         latest = k 
      if laterFound: 
       Found.append(latest) 
     return getKeyFromKeyNames(Found) 
    else: 
     getKeysInFile() 
     getPastAmountDecompiledFromFile(number) 
    return