Search code examples
c#asp.netazurefileshareazure-files

Download Azure File Share Documents Programmatically


I am on a project where the ability to download documents (.pdf's) from an Azure File Storage account would be useful. Is this possible? I am currently only able to output the directories and directory's file content paths as strings, but unable to access the files at those paths, using the Microsoft.Azure namespace.

Additional details: In C#/ASP.NET, being deployed as an Azure Web App

Thank you.

C


Solution

  • Yes, it is possible.

    Here is a demo for your reference:

    My File Share and one picture file as below:

    enter image description here

    My code as below:

        public static void DownloadFromFileStorage()
        {
            CloudStorageAccount account = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=leeliublob;AccountKey=OxxxxxxxxQSy2vkvSi/x/e9l9FhLqayXcbxxxxxJ5Wjkly1DsQPYY5dF2JrAVHtBozbJo29ZrrGJA==;EndpointSuffix=core.windows.net");
            CloudFileClient client = account.CreateCloudFileClient();
    
    
            //get File Share
            CloudFileShare cloudFileShare = client.GetShareReference("myfile");
    
            //get the related directory
            CloudFileDirectory root = cloudFileShare.GetRootDirectoryReference();
            CloudFileDirectory dir = root.GetDirectoryReference("Folder1");
    
            //get the file reference
            CloudFile file = dir.GetFileReference("1.PNG");
    
            //download file to local disk
            file.DownloadToFile("C:\\Test\\1.PNG", System.IO.FileMode.OpenOrCreate);
    
        }
    

    Screenshot of result:

    enter image description here