How can I extract Last Modified Date in MS Azure for a blob in my blob storage
Asked Answered
P

1

7

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?

Poet answered 5/12, 2016 at 14:20 Comment(2)
Hi, I suggest you open your issue on github.com/Azure/azure-storage-python/issues for maximum visibility. Don't forget to post your issue number here please, for other people.Burned
Have you been successful in extracting the zip file in the blob. if yes can you share the snippet for the same.Ulmer
S
11

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>}
Standford answered 6/12, 2016 at 7:6 Comment(2)
Thanks Peter. This worked perfectly well. I think the issue was that I was not writing 'properties' in the correct case and referring the last_modified in an incorrect manner.Poet
in blob we have two modified date, last modified date, and access tier last modified date. which one this code returns?Dyak

© 2022 - 2024 — McMap. All rights reserved.