簡體   English   中英

谷歌雲存儲(Python):如何檢查文件上次修改時間?

[英]Google Cloud Storage(Python): How to check when was file last modified?

我們有一個工作來檢查雲存儲上的文件是否已被修改。 如果是,則它從文件中讀取數據並進一步處理。

我想知道是否有 API 可以檢查雲存儲上的文件上次修改時間。

你可以用boto做到這一點:

>>> import boto
>>> conn = boto.connect_gs()
>>> bucket = conn.get_bucket('yourbucket')
>>> k = bucket.get_key('yourkey')
>>> k.last_modified
'Tue, 04 Dec 2012 17:44:57 GMT'

還有一個用於雲存儲的App Engine Python 接口,但我認為它不會公開您想要的元數據。

Cloud Storage 有一個 API,您可以使用它來獲取對象的創建時間

https://developers.google.com/storage/docs/json_api/v1/objects

App 引擎Cloud Storage 客戶端庫將向您公開此信息。 這個庫也有開發應用服務器支持。 入門有一個例子

您現在可以使用Google Storage官方 Python 庫執行此操作。

from google.cloud import storage


def blob_metadata(bucket_name, blob_name):
    """Prints out a blob's metadata."""
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.get_blob(blob_name)

    print("Blob: {}".format(blob.name))
    print("Bucket: {}".format(blob.bucket.name))
    print("Storage class: {}".format(blob.storage_class))
    print("ID: {}".format(blob.id))
    print("Size: {} bytes".format(blob.size))
    print("Updated: {}".format(blob.updated))
    print("Generation: {}".format(blob.generation))
    print("Metageneration: {}".format(blob.metageneration))
    print("Etag: {}".format(blob.etag))
    print("Owner: {}".format(blob.owner))
    print("Component count: {}".format(blob.component_count))
    print("Crc32c: {}".format(blob.crc32c))
    print("md5_hash: {}".format(blob.md5_hash))
    print("Cache-control: {}".format(blob.cache_control))
    print("Content-type: {}".format(blob.content_type))
    print("Content-disposition: {}".format(blob.content_disposition))
    print("Content-encoding: {}".format(blob.content_encoding))
    print("Content-language: {}".format(blob.content_language))
    print("Metadata: {}".format(blob.metadata))
    print("Temporary hold: ", "enabled" if blob.temporary_hold else "disabled")
    print(
        "Event based hold: ",
        "enabled" if blob.event_based_hold else "disabled",
    )
    if blob.retention_expiration_time:
        print(
            "retentionExpirationTime: {}".format(
                blob.retention_expiration_time
            )
        )

在您的情況下,您將不得不查看blob.updated屬性

我正在使用上面blob.updated提到的解決方案,使用blob.updated來獲取最新文件。 但是存儲桶中有 450 多個文件,此腳本大約需要 6-7 分鍾來瀏覽所有文件並提供最新的最新文件。 我想blob.updated部分需要一些時間來處理。 有沒有更快的方法來做到這一點?

    files = bucket.list_blobs()    
    fileList = [file.name for file in files if '.dat' in file.name]
     
    latestFile = fileList[0]
    latestTimeStamp = bucket.get_blob(fileList[0]).updated
            
    for i in range(len(fileList)):
        
        timeStamp = bucket.get_blob(fileList[i]).updated
        
        if timeStamp > latestTimeStamp:
            latestFile = fileList[i]
            latestTimeStamp = timeStamp
    
    print(latestFile)

暫無
暫無

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

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