【问题标题】:How can I extract Last Modified Date in MS Azure for a blob in my blob storage如何在 MS Azure 中为我的 blob 存储中的 blob 提取上次修改日期
【发布时间】:2017-04-19 23:20:38
【问题描述】:

我对 MS Azure 的世界很陌生。 我正在尝试使用 Python 获取保存在我的 blob 存储中的一堆文件(块 blob)的文件名和最后修改日期。这是我正在使用的代码:

import datetime
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(account_name=account, account_key=acckey,protocol='http', request_session=sess)
blob_service.get_blob_to_path(container, pdfname, pdflocal)
generator = blob_service.list_blobs(container)
filenames = []
for blob in generator:
    print (blob.name)
    pdflocal = './' + blob.name
    properties=blob_service.get_blob_to_path(container, blob.name,pdflocal)
    date_year = datetime.datetime.fromtimestamp(os.path.getmtime("./"+blob.name) ).strftime('%Y-%m-%d %H:%M:%S')
    print (date_year)
    filenames.append(blob.name)
print len(filenames)

这里的问题是,代码尝试创建我的文件的副本,因此最后修改日期更新为当前日期和时间。如何在 Azure ML Studio 中访问实际上次修改的日期和时间?

我阅读了有关 Blob.Properties.LastModified 的信息,但它似乎在 python 中不起作用。这里令人困惑的事情之一是关于转换 CloudBlobs 中的 blob。我不确定这是否必须在 Python 脚本中完成,因为存储资源管理器中的 blob 分为三种类型:块、页面和附加。我在这里遗漏了什么吗?

【问题讨论】:

  • 嗨,我建议你在github.com/Azure/azure-storage-python/issues 上打开你的问题以获得最大的可见性。请不要忘记在此处发布您的问题编号,以供其他人使用。
  • 你是否成功解压了blob中的zip文件。如果是的话,你能分享同样的sn-p吗?

标签: python azure datetime azure-blob-storage last-modified


【解决方案1】:

听起来您想在 Azure ML Studio 中使用 Python 获取 Azure 上 blob 的 last_modified 属性。请尝试使用下面的代码。

for blob in generator:
    last_modified = blob.properties.last_modified
    print(last_modified)

如果你不确定什么属性是否存在,你可以尝试在 Python 交互式环境中编码 <object>.__dict__ 来显示 Python 对象的属性,例如如下。

# Show the properties of a Blob object
>>> blob.__dict__
{'content': '', 'metadata': {}, 'snapshot': None, 'name': 'test.tmp',
 'properties': <azure.storage.blob.models.BlobProperties object at 0x7f4f8f870110>}
# Show the properties of the BlobProperties Object
>>> blob.properties.__dict__
{'content_length': 99831, 'blob_type': 'BlockBlob', 'append_blob_committed_block_count': None, 
 'last_modified': datetime.datetime(2016, 11, 23, 5, 46, 10, tzinfo=tzutc()), 'content_range': None, 'etag': '"0x8D4136407173436"', 'page_blob_sequence_number': None, 'content_settings': <azure.storage.blob.models.ContentSettings object at 0x7f4f8f870510>, 'copy': <azure.storage.blob.models.CopyProperties object at 0x7f4f8f870650>, 'lease': <azure.storage.blob.models.LeaseProperties object at 0x7f4f8f870810>}

【讨论】:

  • 谢谢彼得。这工作得很好。我认为问题在于我没有在正确的情况下编写“属性”并且以不正确的方式引用 last_modified。
  • 在 blob 中,我们有两个修改日期、最后修改日期和访问层最后修改日期。此代码返回哪一个?
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 2021-11-16
  • 2020-04-06
  • 1970-01-01
  • 2017-11-30
  • 2019-04-06
  • 1970-01-01
  • 2017-03-09
相关资源
最近更新 更多