0

How can i download a file from Azure Fileshare using python but i can't modify metadata in new file on local machine? When i download file using FileService or ShareFileClient (azure-storage or azure-storage-file libraries) it change creation date and modified date, so it download new file with metadata changed.

I need to keep original metadata in downloaded file.

#DOWNLOAD A FILE USING FILES SHARE AND SHAREFILECLIENT
account_name = "account_name changed"
account_key = "account_key changed"
share_name = "share_name changed"
file_name = "test.png"

# Initialize the ShareServiceClient
service_client = ShareServiceClient(account_url=f"https://{account_name}.file.core.windows.net", credential=account_key)

# Get a reference to the file
file_client = service_client.get_share_client(share_name).get_directory_client(None).get_file_client(file_name)
file_props = file_client.get_file_properties()
file_metadata = file_props.metadata
local_file_path = "./test.png"

I also try with external apps like rclone and it didn't work.

KT12
  • 549
  • 11
  • 24

1 Answers1

0

When I download a file using FileService or ShareFileClient (azure-storage or azure-storage-file libraries) it changes the creation date and modified date, so it downloads a new file with metadata changed.

The current updated time of the file in the file share is not changed by the download_file() function. After downloading the file, the file's current modified time should remain the same.

It's possible that the file's last modified time in the file share was changed for another reason if you monitor a different last modified time after downloading the file.

You can use the below code to check the downloading file from file share both creation time and modified time.

Code:

from azure.storage.fileshare import ShareServiceClient

connection_string = "your-connection-string"
share_name = "fileshare1"
file_path = "directory1/Icon-60@3x.png"
local_file_path = "./test.png"
# Initialize the ShareServiceClient
service_client = ShareServiceClient.from_connection_string(connection_string)

# Get a reference to the file
share_client = service_client.get_share_client(share_name)
file_client = share_client.get_file_client(file_path)

original_creation_time=file_client.get_file_properties().creation_time
#Get the last modified time of the file before downloading it
original_last_modified = file_client.get_file_properties().last_modified

# Download the file
with open(local_file_path, "wb") as local_file:
    downloaded_file = file_client.download_file()
    local_file.write(downloaded_file.readall())

 # Get the last modified time of the file after downloading it
new_last_modified = file_client.get_file_properties().last_modified
#print(new_last_modified)
new_creation_time=file_client.get_file_properties().creation_time
#print(new_creation_time)
metadata=file_client.get_file_properties().metadata
print(metadata)
# Compare the last modified times
if original_last_modified == new_last_modified:
    print("Last modified time was not changed.")
else:
    print("Last modified time was changed.")

if original_creation_time==new_creation_time:
    print("Creation time Was not changed.")
else:
    print("Creation time was Changed.")

Output:

{'test': 'demo'}
Last modified time was not changed.
Creation time Was not changed.

enter image description here

Portal:

enter image description here

Reference:

Azure Storage File Share client library for Python | Microsoft Learn

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • I have been tested this code but didn't work when i check file properties on windows file explorer it's creation date have been changed. – Diego Buelvas Aug 14 '23 at 15:40
  • `Windows File Explorer` may not always reflect the actual timestamps of the file due to caching and other factors. It is recommended to use the code you provided to check the timestamps of the file directly from the file share to ensure accuracy. – Venkatesan Aug 22 '23 at 13:44