我编写了以下Python脚本,将S3存储桶中的 ALL 文件下载到当前目录中:
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文件(使用“上次修改的列”值?),而不是全部拉下来。
答案 0 :(得分:0)
这似乎可行:
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>