Home > database >  .NET ShareFileItem properties null for Azure File Share
.NET ShareFileItem properties null for Azure File Share

Time:10-07

I am trying to create an azure function that'll clear down files older than a certain age, but when I access the properties of the file they are all null, what am I doing wrong?!

using System;
using System.Collections.Generic;
using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;

namespace somewhere
{
    public static class FileShareCleaner
    {
        [FunctionName("FileShareCleaner")]
        public static void Run([TimerTrigger("*/10 */1 * * * *")]TimerInfo myTimer, ILogger log)
        {
            string connectionString = Environment.GetEnvironmentVariable("FileShareConnectionString");
            string shareName = "files";

            ShareServiceClient shareserviceclient = new ShareServiceClient(connectionString);
            ShareClient shareclient = shareserviceclient.GetShareClient(shareName);

            Queue<ShareDirectoryClient> remaining = new Queue<ShareDirectoryClient>();
            remaining.Enqueue(shareclient.GetRootDirectoryClient());
            while (remaining.Count > 0)
            {
                ShareDirectoryClient dir = remaining.Dequeue();
                foreach (ShareFileItem item in dir.GetFilesAndDirectories())
                {
                    log.LogInformation(item.Name);

                    if (item.IsDirectory)
                    {
                        remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
                    }
                    else
                    {
                        log.LogInformation($"time: {item.Properties.LastModified.ToString()}");
                    }
                        
                }
            }
        }
    }
}

The code finds the files but all the properties are null:

[2021-10-06T10:04:50.048Z] Executing 'FileShareCleaner' (Reason='Timer fired at 2021-10-06T11:04:50.0126493 01:00', Id=af5c7864-4326-4c97-b9d6-82bf98726f4e)
[2021-10-06T10:04:50.341Z] 0304ccf5-4e32-4206-b903-af5acc8652dc.dat
[2021-10-06T10:04:50.344Z] time:
[2021-10-06T10:04:50.347Z] 06716b40-cce4-4ef0-86ec-329dcaeddbf4.dat
[2021-10-06T10:04:50.350Z] time:
[2021-10-06T10:04:50.353Z] 20735b83-d8b2-4110-9ee6-6154b97c154c.dat
[2021-10-06T10:04:50.355Z] time:
[2021-10-06T10:04:50.358Z] 2696a0eb-2aed-4200-b495-0dd2a7152139.dat
[2021-10-06T10:04:50.361Z] time:

CodePudding user response:

You are not doing anything wrong. This is expected behavior.

By default when files and folders are listed in a File Share, only size of the file is returned.

For fetching other properties of the file like last modified or content properties, you will need to get the properties of each file separately.

Update

To get the properties, what you will need to do is create an instance of ShareFileClient using ShareDirectoryClient.GetFileClient and then call GetProperties on that. Your code would look something like below:

while (remaining.Count > 0)
{
    ShareDirectoryClient dir = remaining.Dequeue();
    foreach (ShareFileItem item in dir.GetFilesAndDirectories())
    {
        log.LogInformation(item.Name);

        if (item.IsDirectory)
        {
            remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
        }
        else
        {
            var fileClient = dir.GetFileClient(item.Name);
            var fileProperties = fileClient.GetProperties();
            log.LogInformation($"time: {properties.LastModified.ToString()}");
        }
    }
}
  • Related