2017-08-11 13 views
0

J'essaie de télécharger plusieurs fichiers qui ont été téléchargés pour la dernière fois ou qui ont été téléchargés depuis un seau s3 en utilisant le code python. Utiliser le code ci-dessous ne veut pas que tous les fichiers soient téléchargés?Télécharger plusieurs fichiers qui ont été téléchargés pour la dernière fois ou téléchargés aujourd'hui depuis un seau s3 en utilisant le code python

#!/usr/bin/env python 

import boto 
import sys, os 
from boto.s3.key import Key 
from boto.exception import S3ResponseError 

DOWNLOAD_LOCATION_PATH = os.path.expanduser("~") + "/s3-backup/" 
if not os.path.exists(DOWNLOAD_LOCATION_PATH): 
    print ("Making download directory") 
    os.mkdir(DOWNLOAD_LOCATION_PATH) 


def backup_s3_folder(): 
    BUCKET_NAME = "xxxx" 
    AWS_ACCESS_KEY_ID= os.getenv("xxxxx") # set your AWS_KEY_ID on your environment path 
    AWS_ACCESS_SECRET_KEY = os.getenv("xxxxxx") # set your AWS_ACCESS_KEY on your environment path 
    conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_ACCESS_SECRET_KEY) 
    bucket = conn.get_bucket(BUCKET_NAME) 

    #goto through the list of files 
    bucket_list = bucket.list() 

    for l in bucket_list: 
     key_string = str(l.key) 
     s3_path = DOWNLOAD_LOCATION_PATH + key_string 
     try: 
      print ("Current File is ", s3_path) 
      l.get_contents_to_filename(s3_path) 
     except (OSError,S3ResponseError) as e: 
      pass 
      # check if the file has been downloaded locally 
      if not os.path.exists(s3_path): 
       try: 
        os.makedirs(s3_path) 
       except OSError as exc: 
        # let guard againts race conditions 
        import errno 
        if exc.errno != errno.EEXIST: 
         raise 
if __name__ == '__main__': 
    backup_s3_folder() 
+0

Bienvenue à SO. En sélectionnant un bloc de texte et en cliquant sur le bouton "Code Sample" (ressemble à ceci {}}, vous pouvez marquer le bloc entier comme étant du code et il sera formulé en conséquence. – orangeInk

Répondre

0

D'après ce que je comprends, vous voulez récupérer uniquement les fichiers qui ont été téléchargés « aujourd'hui » et par aujourd'hui, je veux dire le même jour ce code est exécuté en fonction du temps de date à l'OS. Vous pouvez y parvenir en ajoutant une condition à l'instruction foreach pour évaluer le dernier attribut modifié de chaque élément, il serait quelque chose le long de ces lignes:

#!/usr/bin/env python 

import boto 
import sys, os 
from boto.s3.key import Key 
from boto.exception import S3ResponseError 

#Get today's date 
date_today = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0) 

DOWNLOAD_LOCATION_PATH = os.path.expanduser("~") + "/s3-backup/" 
if not os.path.exists(DOWNLOAD_LOCATION_PATH): 
    print ("Making download directory") 
    os.mkdir(DOWNLOAD_LOCATION_PATH) 
def backup_s3_folder(): 
    BUCKET_NAME = "xxxx" 
    AWS_ACCESS_KEY_ID= os.getenv("xxxxx") # set your AWS_KEY_ID on your environment path 
    AWS_ACCESS_SECRET_KEY = os.getenv("xxxxxx") # set your AWS_ACCESS_KEY on your environment path 
    conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_ACCESS_SECRET_KEY) 
    bucket = conn.get_bucket(BUCKET_NAME) 

    #goto through the list of files 
    bucket_list = bucket.list() 

    for l in bucket_list: 
     key_string = str(l.key) 
     s3_path = DOWNLOAD_LOCATION_PATH + key_string 
     try: 
      if boto.utils.parse_ts(l.last_modified) > date_today: 
       print ("Current File is ", s3_path) 
       l.get_contents_to_filename(s3_path) 
     except (OSError,S3ResponseError) as e: 
      pass 
      # check if the file has been downloaded locally 
      if not os.path.exists(s3_path): 
       try: 
        os.makedirs(s3_path) 
       except OSError as exc: 
        # let guard againts race conditions 
        import errno 
        if exc.errno != errno.EEXIST: 
         raise 
if __name__ == '__main__': 
    backup_s3_folder() 

Tout cela ne fait obtenir la date d'aujourd'hui à minuit et vérifier si le dernier attribut modifié du fichier s3 est supérieur à cela (donc le fichier a été importé/modifié "aujourd'hui"). Gardez à l'esprit que vous devrez probablement faire un peu de manipulation avec le fuseau horaire lorsque vous récupérez le dernier attribut modifié, assurez-vous de le convertir dans le même fuseau horaire que le système qui exécute ce script!