簡體   English   中英

使用 Python 從 AWS s3 的子目錄中檢索上次修改的文件

[英]Retrieving last modified file from a subdirectory in AWS s3 using Python

我試圖從 s3 存儲桶中獲取最后修改的文件。 我嘗試了不同的方法來檢索它。 但它總是向我顯示目錄中的所有文件。 有人可以請教嗎?

 def last_updated_file(self):
    bucket_name = "test-bucket"
    conn = boto3.session.Session(profile_name="profile-name")
    s3 = conn.resource('s3')
    test_buc = s3.Bucket(bucket_name)
    for x in test_buc.objects.filter(Prefix='test-file/files/'):
        result = x.last_modified
        print("Result is :", result)

我得到的 output 是

Result is : 2022-01-18 15:52:48+00:00
Result is : 2022-01-18 15:54:11+00:00
Result is : 2022-01-18 15:54:12+00:00
Result is : 2022-01-24 17:40:24+00:00

我只需要最后一個文件“2022-01-24 17:40:24+00:00”

def last_updated_file(self):
bucket_name = "test-bucket"
conn = boto3.session.Session(profile_name="profile-name")
s3 = conn.resource('s3')
test_buc = s3.Bucket(bucket_name)
x = None
for x in test_buc.objects.filter(Prefix='test-file/files/'):
    result = x.last_modified
    print("Result is :", result)
last_modified_file = x.last_modified

這樣我們就可以得到最后更新的文件

暫無
暫無

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

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