Skip to main content
Version: 2.x

ZIO FTP

ZIO FTP is a thin wrapper over (s)Ftp client for ZIO.

Production Ready CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO FTP

Installationโ€‹

In order to use this library, we need to add the following line in our build.sbt file:

libraryDependencies += "dev.zio" %% "zio-ftp" % "0.5.2"

How to use it?โ€‹

  • Imports
import zio.ftp._
  • FTP
// FTP
val unsecureSettings = UnsecureFtpSettings("127.0.0.1", 21, PasswordCredentials("foo", "bar"))

//listing files
Ftp.ls("/").runCollect.provideLayer(unsecure(unsecureSettings))
  • FTPS
// FTPS
val secureSettings = SecureFtpSettings("127.0.0.1", 21, PasswordCredentials("foo", "bar"))

//listing files
SFtp.ls("/").runCollect.provideLayer(secure(secureSettings))
  • SFTP (support ssh key)
val sftpSettings = SecureFtpSettings("127.0.0.1", 22, PasswordCredentials("foo", "bar"))

//listing files
SFtp.ls("/").runCollect.provideLayer(secure(sftpSettings))

Exampleโ€‹

First we need an FTP server, so let's create one:

docker run -d \
-p 21:21 \
-p 21000-21010:21000-21010 \
-e USERS="one|1234" \
-e ADDRESS=localhost \
delfer/alpine-ftp-server

Now we can run the example:

import zio._
import zio.stream._
import zio.ftp._
import zio.ftp.Ftp._

import java.io.IOException

object ZIOFTPExample extends ZIOAppDefault {

private val settings =
UnsecureFtpSettings("127.0.0.1", 21, PasswordCredentials("one", "1234"))

private val myApp: ZIO[Ftp, IOException, Unit] =
for {
_ <- Console.printLine("List of files at root directory:")
resource <- ls("/").runCollect
_ <- ZIO.foreach(resource)(e => Console.printLine(e.path))
path = "~/file.txt"
_ <- upload(
path,
ZStream.fromChunk(
Chunk.fromArray("Hello, ZIO FTP!\nHello, World!".getBytes)
)
)
file <- readFile(path)
.via(ZPipeline.utf8Decode)
.runCollect
_ <- Console.printLine(s"Content of $path file:")
_ <- Console.printLine(file.mkString)
} yield ()

override def run = myApp.provideSomeLayer(unsecure(settings))
}

Support any commands?โ€‹

If you need a method which is not wrapped by the library, you can have access to underlying FTP client in a safe manner by using

import zio._

trait FtpAccessors[+A] {
def execute[T](f: A => T): ZIO[Any, IOException, T]
}

ZIO Chat

๐Ÿ”ฅ Ignite your ZIO journey with ZIO Chat! ๐Ÿ”ฅ Ready to unravel the intricacies of ZIO? As your trusty sidekick, I'm here to light the way. No matter if you're wandering through functional forests or traversing effectful landscapes, I've got answers to keep you on track. Your ZIO adventure starts here โ€“ ask me anything and let's explore together!