gpt4 book ai didi

c# - 将文件从 Azure 存储 blob 移动到 Ftp 服务器

转载 作者:行者123 更新时间:2023-12-02 06:46:29 31 4
gpt4 key购买 nike

我需要将一些文件从 Azure 存储上传到外部 Ftp 服务器。

Azure 有没有办法直接上传这些文件而无需事先下载它们?

最佳答案

您需要使用两个类/库并在此处创建两个方法:

  1. WebClient类将文件从 blob 存储下载到本地驱动器
  2. 用于移动文件的 FTP 库(例如 WinSCP)

WebClient 类:您需要提供以下格式的 URI 参数:https://[accountname].blob.core.windows.net/[containername]/[filetodownloadincludingextension]

下载位置必须是一个变量,作为要上传到 FTP 服务器的文件的原始位置。

        string uri = "https://[accountname].blob.core.windows.net/[containername]/[filetodownloadincludingextension]/";
string file = "file1.txt";
string downloadLocation = @"C:\";

WebClient webClient = new WebClient();
Log("Downloading File from web...");
try
{
webClient.DownloadFile(new Uri(uri+file), downloadLocation);
Log("Download from web complete");
webClient.Dispose();
}
catch (Exception ex)
{
Log("Error Occurred in downloading file. See below for exception details");
Log(ex.Message);
webClient.Dispose();
}
return downloadLocation + file;

下载到本地驱动器后,您需要将其上传到 FTP/SFTP 服务器。为此,您可以使用 WinSCP 库:

        string absPathSource = downloadLocation + file;
string destination = "/root/folder"; //this basically is your FTP path

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{

Protocol = Protocol.Sftp,
HostName = ConfigurationManager.AppSettings["scpurl"],
UserName = ConfigurationManager.AppSettings["scpuser"],
Password = ConfigurationManager.AppSettings["scppass"].Trim(),
SshHostKeyFingerprint = ConfigurationManager.AppSettings["scprsa"].Trim()
};

using (Session session = new Session())
{

//disable version checking
session.DisableVersionCheck = true;

// Connect
session.Open(sessionOptions);

// Upload files
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;

TransferOperationResult transferResult;
transferResult = session.PutFiles(absPathSource, destination, false, transferOptions);

// Throw on any error
transferResult.Check();

// Print results
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
//Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
}
}

如果您想在上传后从本地硬盘删除文件,您可以在上传到 FTP 代码的末尾添加 File.Delete 代码。

关于c# - 将文件从 Azure 存储 blob 移动到 Ftp 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49851795/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com