简体   繁体   中英

How to use Python boto3 to get count of files/object in s3 bucket older than 60 days?

I'm trying to get the count of all object which are older than 60 days? Is there any way to perform a query or any python boto3 method to get this required output?

Here is code to files or object from S3 bucket older than 60 days.

import json
import boto3
import datetime
import time
from time import mktime


client = boto3.client('s3')
response = client.list_objects(Bucket='angularbuildbucket')
print(response)
today_date_time = datetime.datetime.now().replace(tzinfo=None)
print(today_date_time)
    
for file in response.get("Contents"):
    file_name =file.get("Key")
    modified_time = file.get("LastModified").replace(tzinfo=None)
         
    difference_days_delta = today_date_time - modified_time
    difference_days = difference_days_delta.days
    print("difference_days---", difference_days)
    if difference_days > 60:
        print("file more than 60 days older : - ", file_name)

Note : Make sure if you running this code locally to set AWS CLI environment and pass profile rightly.

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