Christopher
Christopher

Reputation: 55

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

Upvotes: 3

Views: 9632

Answers (3)

Anirban Saha
Anirban Saha

Reputation: 1760

This is not a .NET solution. I am including this solution for anyone using Windows and would prefer a really simple solution to download any file(s) from a blob. The easiest way to do this is using the azcopy command. Download the azcopy.exe from here For windows - Start cmd and start the downloaded .exe by going to the downloaded folder.

cd <folder-with-azcopy.exe>
azopy.exe

Then go to your Storage Account -> Under Settings go to Shared Access Signature -> Create and Get the SAS Token.

Run the following in CMD: SAS token starts with ?sig=..., <local-directory-path> is the folder you wish to download everything to (C:/Users/.../download).

azcopy cp 'https://<storage-account-name>.file.core.windows.net/<file-share-name>/<directory-path><SAS-token>' '<local-directory-path>' --recursive=True

Upvotes: -1

Sam Zimmerman
Sam Zimmerman

Reputation: 1

To answer yanivtwin the download to file would be:

file.GetFileReference("1.PNG").DownloadToFile("FolderPath",System.IO.FileMode.OpenOrCreate);

Upvotes: 0

Lee Liu
Lee Liu

Reputation: 2091

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

Upvotes: 6

Related Questions