簡體   English   中英

Running a C# FTP client against FTP server (active mode) from an Azure Function - “500 Syntax error, command unrecognized”

[英]Running a C# FTP client against FTP server (active mode) from an Azure Function - “500 Syntax error, command unrecognized”

我正在嘗試連接到使用 Active 模式的遠程 FTP 服務器。 我已經使用 .NET Core 3.1 實現了一個簡單的 C# FTP 客戶端

var filename = _ftpServerUrl + "/" + targetFilePath;
System.Net.FtpWebRequest request = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(filename);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Proxy = null;
request.Credentials = new NetworkCredential(username, password);
request.KeepAlive = false;
request.UsePassive = false;
request.UseBinary = true;       
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(fileStream))
{
    fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}
request.ContentLength = fileContents.Length;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(fileContents, 0, fileContents.Length);
}

這在 localhost 上的 VS 2019 中運行時效果很好。 但是當代碼在 Azure 中作為 Azure function (v3) 運行時,我從服務器得到以下響應:

500 語法錯誤,命令無法識別

我了解 FTP 主動模式要求客戶端將其公共 IP 地址發送到服務器,然后服務器將嘗試打開和傳入到客戶端的連接。

Azure Function 沙箱中是否有任何東西可以防止這種情況發生?

我什至嘗試使用帶有 static public ZA12A3079E1ACEDB246 的應用服務計划運行我的 Azure Function。

有沒有人使用 Azure 應用服務或 Function 的活動模式成功連接到 FTP 服務器?

FTP 服務器不“使用主動模式” 可以選擇使用主動模式的是 FTP 客戶端。 但是現在防火牆和 NAT 無處不在,使用主動模式幾乎無法工作。 在雲服務、虛擬機的情況下更是如此,對於 Azure 功能這樣的特定虛擬服務更是如此。

FTP 主動模式需要傳入連接。 Azure 防火牆和 NAT 阻止了這種情況。 我希望運行 Azure 函數的虛擬機不會轉發/打開任何傳入端口。 他們為什么會? 它違背了 Azure 功能的目的。 它們旨在運行短期功能,而不是服務器。 主動 FTP 模式在這方面是一頭奇獸。 幾乎沒有任何其他客戶端功能需要傳入端口。

允許傳入連接(如果可能的話,在 Azure 函數中)不是編程問題。 您的代碼沒有任何更改會有所幫助。 一般來說,您應該使用被動模式。

有關一些背景知識,請參閱我關於主動和被動 FTP 模式所需的網絡配置的文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM