简体   繁体   English

增强Python脚本以下载在过去24小时内创建的Amazon S3文件

[英]Enhance Python script to download Amazon S3 files created in last 24 hours

I wrote the following Python script to download ALL files within an S3 Bucket into my current directory: 我编写了以下Python脚本,将S3存储桶中的所有文件下载到当前目录中:

import boto3
import botocore
import os

from boto3.session import Session

ACCESS_KEY='AWS_IAM_AccessKey'
SECRET_KEY='AWS_IAM_SecretKey'

session = Session(aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
myBucket = s3.Bucket('S3_bucketName')

for object in thamesBucket.objects.all():
      myBucket.download_file(object.key, os.path.join(os.curdir, os.path.basename(object.key)))

I'd like to further enhance this script to only pull down S3 files generated within the last 24 hours (using the Last Modified column value?) as opposed to all of them. 我想进一步增强此脚本,以仅下拉最近24小时内生成的S3文件(使用Last Modified列值?),而不是全部文件。

This seems to work: 这似乎可行:

from datetime import datetime, timedelta
from dateutil.tz import tzutc, UTC
import boto3

s3 = boto3.resource('s3', region_name='YOUR-REGION')
bucket = s3.Bucket('YOUR-BUCKET')

for object in bucket.objects.all():
    if object.last_modified > datetime.now(tzutc()) - timedelta(hours = 24):
        <download code here>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:列出最近24小时内创建的具有特定扩展名的子目录中的文件 - Python: List files in subdirectories with specific extension created in last 24 hours 如何从Amazon S3下载上个月创建的文件夹中的所有文件? - How can I download all files from Amazon S3 that are in a folder that was created last month? 使用 Python,递归地为过去 24 小时内创建的 all.jpg 文件创建符号链接 - Using Python, recursively create symbolic links for all .jpg files created within the last 24 hours 如何在 os 目录中获取过去 24 小时内创建的所有文件 - How to get all the files created in last 24 hours in os directory 实时将Json文件存储到Amazon S3的Python脚本 - Python script to store Json files in real time to amazon S3 返回在过去24小时内修改过的Amazon S3存储桶中的所有密钥 - Return all keys from an Amazon S3 bucket which have been modified in the past 24 hours 获取过去 24 小时内创建的记录 - Get records created in last 24 hours 如何创建 python 脚本,以便当目录中的 csv 文件在过去 24 小时内未更新时发送 email? - How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours? 如何使用python和boto3将Amazon S3文件下载到文件夹中的本地计算机上? - How to download Amazon S3 files on to local machine in folder using python and boto3? 传输新文件或最近 24 小时内编辑过的文件 - Transfer files that are new or edited in the last 24 hours
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM