Ich habe eine Azure -Funktions -App, die in C# geschrieben wurde und im dotnet 8 -isolierten Modus ausgeführt wird. Die Funktions -App wird unter einem Linux -App -Serviceplan gehostet und muss den Befehl smbclient aufrufen.public class FileShareDownload
{
private readonly ILogger _logger;
public FileShareDownload(ILogger logger)
{
_logger = logger;
}
[Function("FileShareDownload")]
public async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("Processing GetFileList request.");
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var fileShareRequest = System.Text.Json.JsonSerializer.Deserialize(requestBody);
if (fileShareRequest == null
|| string.IsNullOrEmpty(fileShareRequest.FileShareBasePath)
|| string.IsNullOrEmpty(fileShareRequest.FileShareDirectoryPath)
|| string.IsNullOrEmpty(fileShareRequest.Username)
|| string.IsNullOrEmpty(fileShareRequest.Password))
{
return new BadRequestObjectResult("Invalid request.");
}
// Use smbclient to list files in the file share path
string smbClientCommand = $"smbclient {fileShareRequest.FileShareBasePath} -U {fileShareRequest.Username}%{fileShareRequest.Password} -c \"cd {fileShareRequest.FileShareDirectoryPath.Replace("/", "; cd ")} ; ls\" -d 2";
_logger.LogWarning("Executing Command: " + smbClientCommand);
_logger.LogWarning("Result of date Process " + RunCommandWithBash("date"));
_logger.LogWarning("Result of help Process " + RunCommandWithBash("help"));
var processInfo = new ProcessStartInfo("bash", $"-c \"{smbClientCommand}\"")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = processInfo
};
process.Start();
//string result = await process.StandardOutput.ReadToEndAsync();
//string error = await process.StandardError.ReadToEndAsync();
string result = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
_logger.LogWarning($"smbclient standard output: {result}");
_logger.LogWarning($"smbclient standard error: {error}");
if (process.ExitCode != 0)
{
_logger.LogError($"smbclient error: {error}");
return new StatusCodeResult(StatusCodes.Status500InternalServerError);
}
result = RunCommandWithBash(smbClientCommand);
_logger.LogWarning("New result: " + result);
_logger.LogWarning($"Received response: {result}");
return new OkObjectResult(result);
}
public string RunCommandWithBash(string command)
{
var psi = new ProcessStartInfo();
psi.FileName = "/bin/bash";
psi.Arguments = command;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
using var process = Process.Start(psi);
process.WaitForExit(30000);
var output = process.StandardOutput.ReadToEnd();
return output;
}
}
< /code>
Wenn ich die Funktions -App teste, ist die Antwort aus dem Ausführen der Bash -Befehle immer leer. So sieht es aus:
Wenn ich den Befehl einfügen und in Kudu SSH -Konsole ausführe, funktioniert es gut und gibt mir diese Antwort:
2: < /p>
Kann jemand bitte erklären, warum meine Funktion App entweder die Bash -Befehle nicht richtig ausführt oder mir die Ausgabe nicht richtig zeigt? Wir brauchten eine Funktions -App, die eine Verbindung zu Dateifreigaben auf Azure -VMs und Lesedateien herstellt. Wir haben zunächst versucht, die Funktions -App zu erstellen, mit der MPR.dll zur Verbindung zu Dateifreigaben im Windows App -Serviceplan verwendet wurde, stellten jedoch fest, dass die Azure Sandbox for Windows App -Serviceplan Konnektivität auf Port 445 blockiert, die für SMB verwendet wird. Wir wurden von Microsoft beraten, zum Linux -App -Service -Plan zu wechseln, da er diese Einschränkung nicht hat. Wir können jedoch MPR.dll unter Linux nicht verwenden und stattdessen Smbclient verwenden.
Ausführen eines Linux -Bash -Befehls aus der Azure C# -Funktion App ⇐ Linux
-
- Similar Topics
- Replies
- Views
- Last post