locked
Exporting File Directory Containing Files From Azure To Local Drive RRS feed

  • Question

  • User45690240 posted

    I need a little assistance, I have a web application hosted on Azure that stores files to the directory D:\home\site\UploadeFiles, within this directory, it stores directories with a given uniqueID name, that stores all upload files from an individual.

    What I would Like to do, is write a C# console application to connect to azure and download all the directories to a local directory. I am new new Azure and would like any information is possible that would help me to complete this task.

    Also, if this can be done the console application that i would be write, would be set as a scheduled task to run once a day at a particular time.

    Thanks In Advance

    Wednesday, May 2, 2018 3:13 PM

All replies

  • User283571144 posted

    Hi PsyChotic,

    According to your description, I suggest you could try to use ftp to access the azure web app's folder.

    Then you could send FtpWebRequest to download files inside azure web app ftp folder.

    I suggest you could firstly download the publish file from the web app overview and find the ftp url, user name, user password from the publish file.

    Get the password:

    Then you could use the username and password in below codes: 

     
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Management.Automation;
    using System.Net;
     
    
    namespace ConsoleTest
    {
        class Program
        {
            static void Main()
            {
    
    
                NetworkCredential credentials = new NetworkCredential(@"brandotestforftp\$brandotestforftp", "tFC8z1ocfJ8PSwMloj80yH1t74QwgX1R6H4gWTpNXrH3dkKJFl1EYfsvgMtD");
                string url = "ftp://waws-prod-bay-089.ftp.azurewebsites.windows.net/site/test/";
                DownloadFtpDirectory(url, credentials, @"D:\test\");
            }
    
            static void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
            {
                FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
                listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                listRequest.Credentials = credentials;
    
                List<string> lines = new List<string>();
    
                using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
                using (Stream listStream = listResponse.GetResponseStream())
                using (StreamReader listReader = new StreamReader(listStream))
                {
                    while (!listReader.EndOfStream)
                    {
                        lines.Add(listReader.ReadLine());
                    }
                }
    
                foreach (string line in lines)
                {
                    string[] tokens =
                        line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                    string name = tokens[3];
    
    
                    string localFilePath = Path.Combine(localPath, name);
                    string fileUrl = url + name;
    
                    FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
                    downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    downloadRequest.Credentials = credentials;
    
                    using (FtpWebResponse downloadResponse =
                              (FtpWebResponse)downloadRequest.GetResponse())
                    using (Stream sourceStream = downloadResponse.GetResponseStream())
                    using (Stream targetStream = File.Create(localFilePath))
                    {
                        byte[] buffer = new byte[10240];
                        int read;
                        while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            targetStream.Write(buffer, 0, read);
                        }
                    }
                }
            }
        }
    }
    
    

    Result:

    Best Regards,

    Brando

    Thursday, May 3, 2018 7:41 AM
  • User45690240 posted

    Thanks Brando ZWZ,

    I will give this a try. I was also looking at webjobs, so I think I should try to implement both.

    Tuesday, May 8, 2018 5:58 PM