代码之家  ›  专栏  ›  技术社区  ›  Sanpas

使用FTP从Azure WebJobs发送文件

  •  1
  • Sanpas  · 技术社区  · 7 年前

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

    我已经读完了所有这些帖子,但我没有找到一个方法:

    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

    File download from FTP fails only in Azure web app

    编辑:

    FtpWebRequest reqFTP = null;
            Stream ftpStream = null;
    
            string[] subDirs = directory.Split('/');
    
            string currentDir = publishUrl;
    
            foreach (string subDir in subDirs)
            {
                try
                {
                    currentDir = currentDir + "/" + subDir;
                    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)await reqFTP.GetResponseAsync();
                    ftpStream = response.GetResponseStream();
                    ftpStream.Close();
                    response.Close();
                }
                catch (Exception exception)
                {
    
                    Console.WriteLine(exception.Message);
                    //directory already exist I know that is weak but there is no way to check if a folder exist on ftp...
                }
            }
    

    代码发送文件:

            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(userName, userPWD);
                    client.UploadFile(publishUrl, "STOR", localFileName);
                    return true;
                }
            }
            catch (Exception exception)
            {
                //Console.Error.Write(exception.Message);
                Console.WriteLine(exception.Message);
                return false;
            }
    

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

    当我在本地机器上运行代码时记录(非常好): https://pastebin.com/hBpum8T0

    有什么办法吗? 提前打电话。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Sanpas    7 年前

    糟糕的方式(我不喜欢这样),但这是工作……:

    更改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;
                        }
                    }
                }
            }
        }
    

    像这样更改send file函数

     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;
        }
    

    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;
        }
    

    并更改服务器名称:

    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支持小姐的电话。。。