[英]List files in azure file storage and get the most recent file
I have files in azure file storage, so I listed the files by the date upload and then I want to select the most recent file uploaded.我在 azure 文件存储中有文件,所以我按上传日期列出了文件,然后我想选择最近上传的文件。 So to do this, I created a function that should have returned me, the list of the files.为此,我创建了一个函数,该函数应该返回文件列表。 However when I see the output, it return only one file and the other are missing.但是,当我看到输出时,它只返回一个文件,而另一个文件丢失了。
Here is my code:这是我的代码:
file_service = FileService(account_name='', account_key='')
generator = list(file_service.list_directories_and_files(''))
def list_files_in(generator,file_service):
list_files=[]
for file_or_dir in generator:
file_in = file_service.get_file_properties(share_name='', directory_name="", file_name=file_or_dir.name)
file_date= file_in.properties.last_modified.date()
list_tuple = (file_date,file_or_dir.name)
list_files.append(list_tuple)
return list_files
To get the latest files in azure blob you need to write the logic to get that, below are the code which will give you the same :要获取 azure blob 中的最新文件,您需要编写逻辑来获取它,下面是代码,它们将为您提供相同的:
For Files对于文件
result = file_service.list_directories_and_files(share_name, directory_name)
for file_or_dir in result:
if isinstance(file_or_dir, File):
file = file_service.get_file_properties(share_name, directory_name, file_name, timeout=None, snapshot=None)
print(file_or_dir.name, file.properties.last_modified)
For Blob对于 Blob
from azure.storage.blob import ContainerClient
container = ContainerClient.from_connection_string(conn_str={your_connection_string}, container_name = {your_container_name})
for blob in container.list_blobs():
print(f'{blob.name} : {blob.last_modified}')
You can get the key's and account details from the azure portal for the account access.您可以从用于帐户访问的 azure 门户获取密钥和帐户详细信息。 In this blob.last_modified
will give you latest blob item.在这个blob.last_modified
会给你最新的 blob 项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.