快速发送带有SFTP的CSV文件 [英] Sending CSV file with SFTP in swift

查看:252
本文介绍了快速发送带有SFTP的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管有webfaction的服务器,我希望能够使用FTP或SFTP从我的应用程序向其中发送一个CSV文件.我发现许多应该提供帮助的库,例如 ConnectionKit DLSFPT ,和 LxFTPRequest .但是,它们全都位于Objective-c中,而不是swift的,这使得它们很难在Swift 4中阅读,理解和实现.我已经尝试实现LXFTPRequest,因为我发现了上载的swift实现,这是我的代码:

I have a server hosted with webfaction that I would like to be able to send a csv file to from my app with FTP or SFTP. I have found many libraries that should help like ConnectionKit, NMSSH, DLSFPT, and LxFTPRequest. However, all of them are in objective-c and not swift which makes them hard to read, understand, and implement in Swift 4. I have tried to implement LXFTPRequest since I found a swift implementation for the upload and here is my code:

    let fileName = "user-data.csv"
    guard let path = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).first else {fatalError(ErrorMessageStrings.couldntAccessDocs.rawValue)}
    let fileURL = path.appendingPathComponent(fileName)

    let folderLocation = "/home/path/"
    let uploadUrl = URL(string: "ftp://server-name.webfaction.com" + folderLocation)

    let request = LxFTPRequest.upload()
    request?.serverURL = uploadUrl
    request?.localFileURL = fileURL
    request?.username = "username"
    request?.password = "password"

    request?.successAction = { (resultClass, result) in
        print("File uploaded")
    }

    request?.failAction = { (domain, error, errorMessage) in
        print(error)
        print(errorMessage?.description)
        fatalError("Connection could not be made. Action was not completed.")
    }

    request?.progressAction = {(_ totalSize: Int, _ finishedSize: Int, _ finishedPercent: CGFloat) -> Void in
        print(finishedPercent)
    }

    request?.start()`

使用它,我几乎可以正常工作,但最终出现550错误未执行请求的操作.文件不可用(例如,未找到文件,无法访问)."浏览webfaction文档时,我会感觉只能通过SFTP发送文件,而该框架不支持该文件.

Using this I almost get it to work but I end up with a 550 error "Requested action not taken. File unavailable (e.g., file not found, no access)." Looking through webfaction documentation I get the feeling that I can only send files through SFTP, which this framework doesnt support.

医生说要与FTP连接(仅适用于Shell用户),请将连接类型替换为FTP,将端口号替换为21".我假设由于我正在从我的应用程序发送数据,因此它不算作外壳用户,因此FTP不会授予我访问权限(我在这里可能是错误的).如果是这种情况,我将如何使用其他库通过Swift而不是Objective-C通过SFTP发送文件?

The doc says "To connect with FTP (for shell users only), substitute the connection type with FTP and the port number with 21." I am assuming since I am sending data from my app it does not count as a shell user and so FTP doesn't grant me access (I may be wrong here). If that is the case how would I go about using the other libraries to send my file over SFTP using Swift and not objective-c?

推荐答案

我最终使用了NMSSH,并在Swift中使用它并没有我想象的那么复杂.

I ended up using NMSSH and using it in Swift it wasn't as complicated as I thought.

 let session = NMSSHSession.init(host: serverHost, port: xx, andUsername: serverUsername)
    session.connect()
    if session.isConnected{
        session.authenticate(byPassword: serverPasswordString)
        if session.isAuthorized == true {
            let sftpsession = NMSFTP(session: session)
            sftpsession.connect()
            if sftpsession.isConnected {
                sftpsession.writeFile(atPath: csvFileURL.path, toFileAtPath: folderLocation)
            }
        }
    }

这篇关于快速发送带有SFTP的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆