ZIO FTP is a thin wrapper over (s)Ftp client for ZIO.
In order to use this library, we need to add the following line in our build.sbt
file:
libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.2"
- Imports
import zio.ftp._
- FTP
// FTP
val unsecureSettings = UnsecureFtpSettings("127.0.0.1", 21, FtpCredentials("foo", "bar"))
//listing files
Ftp.ls("/").runCollect.provideLayer(unsecure(unsecureSettings))
- FTPS
// FTPS
val secureSettings = SecureFtpSettings("127.0.0.1", 21, FtpCredentials("foo", "bar"))
//listing files
SFtp.ls("/").runCollect.provideLayer(secure(secureSettings))
- SFTP (support ssh key)
val sftpSettings = SecureFtpSettings("127.0.0.1", 22, FtpCredentials("foo", "bar"))
//listing files
SFtp.ls("/").runCollect.provideLayer(secure(sftpSettings))
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, FtpCredentials("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))
}
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]
}
Learn more on the ZIO FTP homepage!
For the general guidelines, see ZIO contributor's guide.
See the Code of Conduct