簡體   English   中英

如何使用 Python boto3 獲取 s3 存儲桶中超過 60 天的文件/對象的計數?

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

我正在嘗試計算所有超過 60 天的 object 的數量? 有什么方法可以執行查詢或任何 python boto3 方法來獲得所需的 output?

這是來自超過 60 天的 S3 存儲桶中的文件或 object 的代碼。

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)

注意:確保您是否在本地運行此代碼以設置 AWS CLI 環境並正確傳遞配置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM