【问题标题】:Google Cloud Storage(Python): How to check when was file last modified?谷歌云存储(Python):如何检查文件上次修改时间?
【发布时间】:2012-12-08 18:14:33
【问题描述】:

我们有一项工作是检查云存储上的文件是否已被修改。如果是,则从文件中读取数据并进一步处理。

我想知道是否有 API 可以检查云存储上的文件的上次修改时间。

【问题讨论】:

    标签: python google-app-engine google-cloud-storage


    【解决方案1】:

    Cloud Storage 有一个 API,您可以使用它来获取对象的创建时间

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

    【讨论】:

    【解决方案2】:

    您可以使用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 interface,但我认为它不会公开你想要的元数据。

    【讨论】:

      【解决方案3】:

      App 引擎Cloud Storage client library 将向您公开此信息。该库还具有开发应用服务器支持。入门有一个example

      【讨论】:

      • 是的,他们似乎正在弃用 Cloud Storage API 以支持新的客户端库。
      【解决方案4】:

      您现在可以使用official Python lib for Google Storage 执行此操作。

      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 属性

      【讨论】:

        【解决方案5】:

        我正在使用@orby 上面提到的解决方案,使用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)
        

        【讨论】:

        • 您不必调用bucket.get_blob(fileList[i]).updated,因为如果您将整个 blob 放入列表中,而不仅仅是文件名,那么您已经在 fileList 中有 blob。然后你可以拨打i.updated
        • 这将使用秒数:files = bucket.list_blobs() fileList = [file for file in files if '.dat' in file.name] latestFile = None for i in fileList: if not latestFile: latestFile = i continue if i.updated > latestFile.updated: latestFile = i print(latestFile)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-10
        • 2019-06-09
        相关资源
        最近更新 更多