本文介绍了增强 Python 脚本以下载过去 24 小时内创建的 Amazon S3 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下 Python 脚本来将 S3 存储桶中的所有文件下载到我的当前目录中:

I wrote the following Python script to download ALL files within an S3 Bucket into my current directory:

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)))

我想进一步增强此脚本以仅下拉过去 24 小时内生成的 S3 文件(使用 Last Modified 列值?)而不是所有这些文件.

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.

推荐答案

这似乎有效:

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>

这篇关于增强 Python 脚本以下载过去 24 小时内创建的 Amazon S3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 14:20