c# - 如何在 c# cake build 中创建任务以使用 FTP 上传文件?

标签 c# ftp cakebuild

我注意到蛋糕支持HTTP操作,但是没有FTP操作,你知道如何创建一个通过FTP上传文件的任务吗?

最佳答案

目前 Cake 中没有任何构建提供使用 FTP 协议(protocol)传输文件的功能。

虽然如果您在“完整 CLR”上运行 Cake,您可以使用 FtpWebRequest 中内置的 .NET 框架来上传文件。 FtpWebRequest 尚未移植到 .NET Core,因此如果您正在运行 Cake.CoreCLR

您可以通过使用静态 FTPUpload 实用方法创建一个 ftp.cake 来实现此目的,您可以从 build.cake 中重用该方法> 文件。

示例 ftp.cake

public static bool FTPUpload(
    ICakeContext context,
    string ftpUri,
    string user,
    string password,
    FilePath filePath,
    out string uploadResponseStatus
    )
{
    if (context==null)
    {
        throw new ArgumentNullException("context");
    }

    if (string.IsNullOrEmpty(ftpUri))
    {
        throw new ArgumentNullException("ftpUri");
    }

    if (string.IsNullOrEmpty(user))
    {
        throw new ArgumentNullException("user");
    }

    if (string.IsNullOrEmpty(password))
    {
        throw new ArgumentNullException("password");
    }

    if (filePath==null)
    {
        throw new ArgumentNullException("filePath");
    }

    if (!context.FileSystem.Exist(filePath))
    {
        throw new System.IO.FileNotFoundException("Source file not found.", filePath.FullPath);
    }

    uploadResponseStatus = null;
    var ftpFullPath = string.Format(
        "{0}/{1}",
        ftpUri.TrimEnd('/'),
        filePath.GetFilename()
        );
    var ftpUpload = System.Net.WebRequest.Create(ftpFullPath) as System.Net.FtpWebRequest;

    if (ftpUpload == null)
    {
        uploadResponseStatus = "Failed to create web request";
        return false;
    }

    ftpUpload.Credentials = new System.Net.NetworkCredential(user, password);
    ftpUpload.KeepAlive = false;
    ftpUpload.UseBinary = true;
    ftpUpload.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
    using (System.IO.Stream
        sourceStream = context.FileSystem.GetFile(filePath).OpenRead(),
        uploadStream = ftpUpload.GetRequestStream())
    {
        sourceStream.CopyTo(uploadStream);
        uploadStream.Close();
    }

    var uploadResponse = (System.Net.FtpWebResponse)ftpUpload.GetResponse();
    uploadResponseStatus = (uploadResponse.StatusDescription ?? string.Empty).Trim().ToUpper();
    uploadResponse.Close();
    return  uploadResponseStatus.Contains("TRANSFER COMPLETE") ||
                     uploadResponseStatus.Contains("FILE RECEIVE OK");
}

示例用法 build.cake

#load "ftp.cake"

string ftpPath = "ftp://ftp.server.com/test";
string ftpUser = "john";
string ftpPassword = "top!secret";
FilePath sourceFile = File("./data.zip");


Information("Uploading to upload {0} to {1}...", sourceFile, ftpPath);
string uploadResponseStatus;
if (!FTPUpload(
        Context,
        ftpPath,
        ftpUser,
        ftpPassword,
        sourceFile,
        out uploadResponseStatus
    ))
{
    throw new Exception(string.Format(
        "Failed to upload {0} to {1} ({2})",
        sourceFile,
        ftpPath,
        uploadResponseStatus));
}

Information("Successfully uploaded file ({0})", uploadResponseStatus);

示例输出

Uploading to upload log.cake to ftp://ftp.server.com/test...
Successfully uploaded file (226 TRANSFER COMPLETE.)

FtpWebRequest 非常基础,因此您可能需要对其进行调整以适应您的目标 ftp 服务器,但以上内容应该是一个很好的起点。

关于c# - 如何在 c# cake build 中创建任务以使用 FTP 上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39597416/

相关文章:

docker - FTP 文件到远程服务器上的 docker 容器中

c# - 将文件移动到 FTP 上的父文件夹

arrays - 如何将字符串数组传递到 ps1 文件?

c# - 如何在执行 powershell 脚本的 msdeploy post sync 命令中转义引号?

c# - 在 Cake 构建脚本中提交计数?

c# - 将 IwebElement 传递到 html 敏捷包 C#

c# - 具有大列表的并行 Foreach 抛出异常

c# - 在 TLS : The remote server return an error : 234 AUTH TLS OK 上使用 FtpWebRequest 的问题

c# - 在生产应用程序中使用 SqlBulkInsert

c# - Json.NET 反序列化对象返回 null