简体   繁体   中英

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:

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM