How to get file metadata from Azure File Share?

1.2k views Asked by At

I'm trying to get File Share Metadata from Azure file share using npm storage-file-share.

Code snippet as below, but didn't find anything in documentation where to get metadata.

Is there any way to get metadata from azure library or has to call rest api?

const serviceClient = new ShareServiceClient(
      `https://${storageAccountName}.file.core.windows.net`,
      credential
    );
    const directoryClient  = serviceClient.getShareClient(shareName).getDirectoryClient(directoryPath);
    const dirIter = directoryClient.listFilesAndDirectories();
    const list = [];
    for await (const item of dirIter) {
        list.push({name: item.name, metadata????}); // Need item metadata 
    }
2

There are 2 answers

0
Ivan Glasenberg On BEST ANSWER

You can use the getProperties method for fetching metadata for the file and directory. Here is the definition of this method:

Returns all user-defined metadata, standard HTTP properties, and system properties for the file. It does not return the content of the file.

So in your code -> inside for await (const item of dirIter), you need to determine if it's a file or directory, then call the getProperties() method. The sample code looks like below:

for await (const item of dirIter) {
      if (item.kind === "directory") {
        
        const mydirectory = directoryClient.getDirectoryClient(item.name);
        var diretory_properties = await mydirectory.getProperties();

        //for test, you can print out the metadata
        console.log(diretory_properties.metadata);

        //here, you can write code to add the metadata in your list

      } else {
        
        const myfile=directoryClient.getFileClient(item.name);
        var the_properties = await myfile.getProperties();

        //for test, you can print out the metadata
        console.log(the_properties.metadata)

       //here, you can write code to add the metadata in your list

      }
    }
0
Sajeetharan On

You can use the method getShareMetadata

fileService.getShareMetadata(shareName, function (error, result, response) {