使用FTP从Azure WebJobs发送文件

时间:2018-10-18 13:42:19

标签: c# azure ftp azure-webjobs

当我尝试使用Azure WebJobs的FTP发送文件时,我遇到1个问题。 交替抛出“远程服务器返回错误:(530)未登录。”,我的代码在localhost(在开发计算机上)上运行良好。

我已经阅读了所有这些文章,但没有找到方法:

FTPWebRequest 530 Error: Not Logged in issue

FTP The remote server returned an error: (530) Not logged in

Fetching files from FTP server via Azure webjobs

Ftp to external server in azure webjob not working

其他,但是我的应用程序服务中只有1个webjobs,并且在作业运行时CPU占30%。 File download from FTP fails only in Azure web app

编辑: 代码制作目录

CMake Error: The source directory "C:/Users/pro12/Desktop/libwebsockets/build" does not appear to contain CMakeLists.txt.

代码发送文件:

[ngStyle]="{'height': 'calc(100% - 57px)'"}

并在Azure WebApp WebJobs中运行代码时记录日志(失败): https://pastebin.com/vgTxqT5p

并在我在本地计算机上运行代码时记录(工作出色): https://pastebin.com/hBpum8T0

我认为解决方法是针对WebJobs应用程序样式的,而正常功能并未等待。我将更改我的代码,以对所有WebJobs程序使用Async Await方法。

有什么办法吗? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

不好的方式(我不喜欢这样),但是它是有效的.....:

像这样更改Make Directory功能:

public static void MakeFTPDir(string publishUrl, string userName, string userPWD, string directory)
    {
        FtpWebRequest reqFTP = null;
        Stream ftpStream = null;

        string[] subDirs = directory.Split('/');

        string currentDir = publishUrl;

        foreach (string subDir in subDirs)
        {
            bool isNotCreated = true;
            int iTentative = 0;
            currentDir = currentDir + "/" + subDir;
            while (isNotCreated)
            {
                iTentative++;
                try
                {
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
                    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                    reqFTP.UseBinary = true;
                    reqFTP.UsePassive = true;
                    reqFTP.KeepAlive = true;
                    reqFTP.Credentials = new NetworkCredential(userName, userPWD);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    ftpStream = response.GetResponseStream();
                    ftpStream.Close();
                    response.Close();
                }
                catch(WebException webException)
                {
                    FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                    if(excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                    {
                        Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                        isNotCreated = true;
                    }
                    else
                    {
                        Console.WriteLine(webException.Message);
                        isNotCreated = false;
                    }
                }
                catch (Exception exception)
                {
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    if (response.StatusCode == FtpStatusCode.NotLoggedIn)
                    {
                        Console.WriteLine("Exception ==> NotLoggedIn >> Tentative :" + iTentative);
                        isNotCreated = true;
                    }
                    else
                    {
                        Console.WriteLine(exception.Message);
                        isNotCreated = false;
                    }
                }
            }
        }
    }

更改发送文件功能

 public static bool SendFtpFile(string publishUrl, string userName, string userPWD, string localFileName)
    {
        bool isNotCreated = true;
        int iTentative = 0;
        while (isNotCreated)
        {
            iTentative++;
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(userName, userPWD);
                    client.UploadFile(publishUrl, "STOR", localFileName);
                    return true;
                }
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNotCreated = true;
                }
                else
                {
                    Console.WriteLine(webException.Message);
                    return false;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
                return false;
            }
        }
        return true;
    }

更改IfFileExistOnServer:

public static bool CheckIfFileExistsOnServer(string publishUrl, string userName, string userPWD, string fileName)
    {
        bool isNoCheck = true;
        int iTentative = 0;
        string azureBotUrl = publishUrl + "/" + fileName;

        while (isNoCheck)
        {
            iTentative++;
            try
            {
                var request = (FtpWebRequest)WebRequest.Create(azureBotUrl);
                request.Credentials = new NetworkCredential(userName, userPWD);
                request.UseBinary = true;
                request.UsePassive = true;
                request.KeepAlive = true;
                request.Method = WebRequestMethods.Ftp.GetFileSize;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                return true;
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    return false;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNoCheck = true;
                }
                else
                {
                    return false;
                }
            }
        }
        return false;
    }

并更改RenameFileOnServer:

public static bool RenameFileOnServer(string publishUrl, string userName, string userPWD, string sourceFileName, string newFileName)
    {
        bool isNoRenameFile = true;
        int iTentative = 0;
        FtpWebRequest ftpRequest = null;
        FtpWebResponse ftpResponse = null;
        string azureBotUrl = publishUrl + "/" + sourceFileName;
        while (isNoRenameFile)
        {
            iTentative++;
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(azureBotUrl);
                ftpRequest.Credentials = new NetworkCredential(userName, userPWD);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.Rename;
                ftpRequest.RenameTo = newFileName.Split('\\')[newFileName.Split('\\').Length - 1];
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
                return true;
            }
            catch (WebException webException)
            {
                FtpWebResponse excWebResponse = (FtpWebResponse)webException.Response;
                if (excWebResponse.StatusCode == FtpStatusCode.NotLoggedIn)
                {
                    Console.WriteLine("WebException ==> NotLoggedIn >> Tentative :" + iTentative);
                    isNoRenameFile = true;
                }
                else
                {
                    return false;
                }
                Console.WriteLine(webException.Message);
            }
            catch (Exception)
            {
                return false;
            }
        }
        return false;
    }

我正在等待Azure支持女士的电话...