Home > Blockchain >  How can I know the dates of the files uploaded in azure file share
How can I know the dates of the files uploaded in azure file share

Time:10-22

I have a file share in azure, and I want to list the content of the files, as well the date of the upload, so that I can see the most recent files uploaded. I managed to list the files, however I can not see the dates of the upload. Here is my code:

from azure.storage.file import FileService

file_service = FileService(account_name='', account_key='')
generator = list(file_service.list_directories_and_files(''))


try:
    for file_or_dir in generator:
        properties= file_service.get_file_properties(share_name='', directory_name="", file_name=file_or_dir.name)

        print(file_or_dir.name, file_or_dir.properties.__dict__)

except ResourceNotFoundError as ex:
    print('ResourceNotFoundError:', ex.message)

When I use the __dict__properties, I got this result:

file_name.zip {'last_modified': None, ...}

UPDATE

with this code, it works:

from azure.storage.file import FileService


file_service = FileService(account_name='', account_key='')
generator = list(file_service.list_directories_and_files(''))


try:
    for file_or_dir in generator:
        file_in = file_service.get_file_properties(share_name='', directory_name="", file_name=file_or_dir.name)
        print(file_or_dir.name, file_in.properties.last_modified)

except ResourceNotFoundError as ex:
    print('ResourceNotFoundError:', ex.message)

CodePudding user response:

This is expected behavior. When you list files and directories in an Azure File Share, very minimal information is returned. For files, only the file name and size is returned.

To get other properties of a file, you will need to separately call get_file_properties for each file in the list. The result of this operation will contain the last modified date of the file.

Update

Please try something like (untested code):

try:
    for file_or_dir in generator:
        properties = file_service.get_file_properties(share_name="share-name", directory_name="", file_name=file_or_dir.name)
        print(file_or_dir.name, properties.__dict__)
  • Related