问题
I am pretty new to the world of MS Azure. I am trying to get the filenames and the last modified date for a bunch of files (block blobs) kept in my blob storage using Python. Here is the code that I am using:
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)
The problem here is, that the code tries to create a copy of my files and hence the last modified date is updated to the current date and time. How can I access the actual last modified date and time in Azure ML Studio?
I read about Blob.Properties.LastModified but it doesn't seem to work in python. One of the confusing things here was about converting the blobs in CloudBlobs. I am not sure if this has to be done within the Python script because the blobs in the Storage Explorer are of three types: Block, Page and Append. Am I missing something here?
回答1:
It sounds like that you want to get the last_modified
property of a blob on Azure using Python in Azure ML Studio. Please try to use the code below.
for blob in generator:
last_modified = blob.properties.last_modified
print(last_modified)
And you can try to code <object>.__dict__
in Python interactive env to show the properties of a Python object if you are not sure what property whether or not exists, for example as below.
# 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>}
来源:https://stackoverflow.com/questions/40976398/how-can-i-extract-last-modified-date-in-ms-azure-for-a-blob-in-my-blob-storage