![](/img/trans.png)
[英]Python: List files in subdirectories with specific extension created in last 24 hours
[英]Enhance Python script to download Amazon S3 files created in last 24 hours
我編寫了以下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)))
我想進一步增強此腳本,以僅下拉最近24小時內生成的S3文件(使用Last Modified列值?),而不是全部文件。
這似乎可行:
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.