Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 返回Amazon S3存储桶中在过去24小时内修改过的所有密钥_Python_Amazon S3_Boto3 - Fatal编程技术网

Python 返回Amazon S3存储桶中在过去24小时内修改过的所有密钥

Python 返回Amazon S3存储桶中在过去24小时内修改过的所有密钥,python,amazon-s3,boto3,Python,Amazon S3,Boto3,情况是这样的:我远程工作,大多数时候人们都在向AmazonS3实例添加数据集。这些数据集中的每一个都需要一些非常相似的处理任务,我可以用一些非常简单的python自动完成这些任务。但是,我似乎无法使用修改后的日期来隔离过去24小时内添加到S3中的数据集。以下是我到目前为止的情况: import boto3 from boto3.session import Session ACCESS_KEY = xxxx SECRET_KEY = xxxx session = Session(aws_acc

情况是这样的:我远程工作,大多数时候人们都在向AmazonS3实例添加数据集。这些数据集中的每一个都需要一些非常相似的处理任务,我可以用一些非常简单的python自动完成这些任务。但是,我似乎无法使用修改后的日期来隔离过去24小时内添加到S3中的数据集。以下是我到目前为止的情况:

import boto3 
from boto3.session import Session
ACCESS_KEY = xxxx
SECRET_KEY = xxxx
session = Session(aws_access_key_id=ACCESS_KEY, 
aws_secret_access_key=SECRET_KEY)
s3 = session.resource('s3')
s3_client = boto3.client('s3')
def get_all_s3_keys(bucket):
    keys = []
    kwargs = {'Bucket': bucket}
    while True:
        resp = s3_client.list_objects_v2(**kwargs)
        for obj in resp['Contents']:
            keys.append(obj['Key'])
        try:
            kwargs['ContinuationToken'] = resp['NextContinuationToken']
        except KeyError:
            break
    return keys

bucket_keys = get_all_s3_keys('mybucket')
recnt_keys = [key for key in bucket_keys if 'Temp' in key]
这将返回“mybucket”中包含单词“Temp”的所有键,但这显然对修改日期没有帮助。一旦我得到最近修改过的密钥列表,我希望能够迭代并将它们下载到预定的本地路径

有什么想法吗

谢谢

请尝试此片段(只需获取所有项目,然后进行筛选):

导入boto3
导入日期时间
s3=boto3.resource('s3')
s3_bucket=s3.bucket('mybucket'))
items=[s3_bucket.objects.filter()]中的item for item#获取它们
现在=datetime.datetime.now(datetime.timezone.utc)
td=datetime.timedelta(小时=24)
last_24_hours_keys=[item.key for items in items if now-item.last_modified

嗯。

哇!谢谢你的建议@Matt Messersmith。我正在使用Python 2(dang Esri Python安装-但我需要arcpy)。我将在下面为python 2添加一些轻微的调整。必须使用pytz而不是datetime.timezone.utc

s3 = boto3.resource('s3')  s3_bucket = s3.Bucket('bucket') 
items = [item for item in s3_bucket.objects.filter()]  
now = datetime.datetime.now(pytz.utc)  
td = datetime.timedelta(hours=24)  
last_24_hours_keys = [item.key for item in items if now - item.last_modified < td]  
print last_24_hours_keys
s3=boto3.resource('s3')s3\u bucket=s3.bucket('bucket'))
items=[s3_bucket.objects.filter()中的item对应的item]
now=datetime.datetime.now(pytz.utc)
td=datetime.timedelta(小时=24)
last_24_hours_keys=[item.key for items in items if now-item.last_modified
s3 = boto3.resource('s3')  s3_bucket = s3.Bucket('bucket') 
items = [item for item in s3_bucket.objects.filter()]  
now = datetime.datetime.now(pytz.utc)  
td = datetime.timedelta(hours=24)  
last_24_hours_keys = [item.key for item in items if now - item.last_modified < td]  
print last_24_hours_keys