Jeff
Jeff

Reputation: 36583

Azure File Storage include file metadata in list request

Is there a way to get Azure to include IListFileItem metadata when calling ListFilesAndDirectories (https://learn.microsoft.com/en-us/dotnet/api/microsoft.windowsazure.storage.file.cloudfiledirectory.listfilesanddirectories?view=azure-dotnet)?

I'm making a simple backup script that leverages file metadata and want the script to be as fast and use as few requests as possible. Therefore I do not want to issue a subsequent request for each file to fetch metadata.

Upvotes: 1

Views: 1062

Answers (2)

Brando Zhang
Brando Zhang

Reputation: 28387

As far as I know, the ListFilesAndDirectories method will call the List Directories and Files rest api. It will send the request to list the file and directory. But the result won't contain the file and directory metadata.

The response is like this:

<?xml version="1.0" encoding="utf-8"?>  
<EnumerationResults ServiceEndpoint="https://myaccount.file.core.windows.net/” ShareName="myshare" DirectoryPath="directory-path">  
  <Marker>string-value</Marker>  
  <MaxResults>int-value</MaxResults>
  <Prefix>prefix-value</Prefix>  
  <Entries>  
    <File>  
      <Name>file-name</Name>  
      <Properties>  
        <Content-Length>size-in-bytes</Content-Length>  
      </Properties>  
    </File>  
    <Directory>  
      <Name>directory-name</Name>  
    </Directory>  
  </Entries>  
  <NextMarker />  
</EnumerationResults>  

So if you want to get the matedata of the file and directory, the only way is sending another request(call FetchAttributes method) for each files to get the files or directories' matadata.

Code like this:

        foreach (IListFileItem item in result)
        {
            if (item is CloudFile)
            {
                var cloudFile = (CloudFile)item;
                cloudFile.FetchAttributes();
                // You can now access metadata and properties
                var rest = cloudFile.Metadata;
                //cloudFile.Properties
            }
            else if (item is CloudFileDirectory)
            {
                var cloudFileDirectory = (CloudFileDirectory)item;
                // You can now access metadata and properties
                cloudFileDirectory.FetchAttributes();
               var rest = cloudFileDirectory.Metadata;
                //cloudFileDirectory.Metadata
                //cloudFileDirectory.Properties
            }
        }

Upvotes: 1

Gaurav Mantri
Gaurav Mantri

Reputation: 136369

As of today it is not possible. You will need to make a separate call for each file and directory returned in the listing result to fetch the metadata.

Cloud​File​Directory.​List​Files​And​Directories is essentially a wrapper over List Directories and Files REST API call and this particular operation does not take a request parameter where you can tell the storage service to return file/directory metadata (you could do such thing when listing blobs in a blob container though).

Upvotes: 3

Related Questions