3

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

Nancy
  • 26,865
  • 3
  • 18
  • 34
Christopher
  • 55
  • 1
  • 7
  • 1
    Yes, it is possible to download these files. Are you just looking for a sample code? If that's the case, check this out https://github.com/Azure/azure-storage-net/blob/master/Samples/GettingStarted/VisualStudioQuickStarts/DataFileStorage/Program.cs. – Evandro de Paula May 31 '18 at 02:55

3 Answers3

5

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

Lee Liu
  • 1,981
  • 1
  • 12
  • 13
  • To have this working do we need to have the File Share called "myfile" pre-setup? – Rahatur Jun 13 '19 at 08:41
  • i get a compile error : 'IListFileItem' does not conain a defenition for 'DownloadToFile' and no extention method 'DownloadToFile' accepting a first argument of type 'IListFileItem' could be found (are you missing a using directive or an assembly reference?) when I try the download to file line , any ideas to why is that ? – yanivtwin Dec 24 '19 at 12:26
0

To answer yanivtwin the download to file would be:

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

-1

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
Anirban Saha
  • 1,350
  • 2
  • 10
  • 38
  • 1
    the OP was asking programmatically – Nick Turner Dec 31 '20 at 15:30
  • Microsoft has created an entire library for handling and working with all of the Azure Storage Containers. Also, we aren't 100% sure if OP is looking for a .Net core option, which would make using a .exe hard to do on a Linux server. Also, it can be tough to programmatically get progress from an external executable can become a programmatic nightmare. Also, as stated above, OP was requesting code as a solution. – Kevin B Burns Sep 16 '21 at 19:28
  • Yes Kevin, I agree. I was facing a similar problem and wanted an easy way to download files to my Windows system. I included this in case it helps someone else facing a similar issue and they stumble across this question. But yes, I agree. Point well taken. – Anirban Saha Sep 17 '21 at 03:42