FTP Connector
The FTP connector provides Akka Stream sources to connect to FTP, FTPs and SFTP servers. Currently, two kinds of sources are provided:
- one for browsing or traversing the server recursively and,
- another for retrieving files as a stream of bytes.
Artifacts
- sbt
-
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-ftp" % "0.9"
- Maven
-
<dependency> <groupId>com.lightbend.akka</groupId> <artifactId>akka-stream-alpakka-ftp_2.12</artifactId> <version>0.9</version> </dependency>
- Gradle
-
dependencies { compile group: "com.lightbend.akka", name: "akka-stream-alpakka-ftp_2.12", version: "0.9" }
Usage
Configuring the connection settings
In order to establish a connection with the remote server, you need to provide a specialized version of a RemoteFileSettings instance. It’s specialized as it depends on the kind of server you’re connecting to: FTP, FTPs or SFTP.
- Scala
-
val settings = FtpSettings( InetAddress.getByName("localhost"), getPort, AnonFtpCredentials, binary = true, passiveMode = true )
- Java
-
final FtpSettings settings = new FtpSettings( InetAddress.getByName("localhost"), getPort(), FtpCredentials.createAnonCredentials(), false, // binary true // passiveMode );
The configuration above will create an anonymous connection with a remote FTP server in passive mode. For both FTPs and SFTP servers, you will need to provide the specialized versions of these settings: FtpsSettings or SftpSettings respectively.
For non-anonymous connection, please provide an instance of NonAnonFtpCredentials instead.
For connection using a private key, please provide an instance of SftpIdentity to SftpSettings.
Traversing a remote FTP folder recursively
In order to traverse a remote folder recursively, you need to use the ls
method in the FTP API:
- Scala
-
protected def listFiles(basePath: String): Source[FtpFile, NotUsed] = Ftp.ls(basePath, settings)
- Java
-
public Source<FtpFile, NotUsed> getBrowserSource(String basePath) throws Exception { return Ftp.ls(basePath, settings()); }
This source will emit FtpFile elements with no significant materialization.
For both FTPs and SFTP servers, you will need to use the FTPs
and SFTP
API respectively.
Retrieving files
In order to retrieve a remote file as a stream of bytes, you need to use the fromPath
method in the FTP API:
- Scala
-
protected def retrieveFromPath(path: String): Source[ByteString, Future[IOResult]] = Ftp.fromPath(path, settings)
- Java
-
public Source<ByteString, CompletionStage<IOResult>> getIOSource(String path) throws Exception { return Ftp.fromPath(path, settings()); }
This source will emit ByteString elements and materializes to Future in Scala API and CompletionStage in Java API of IOResult when the stream finishes.
For both FTPs and SFTP servers, you will need to use the FTPs
and SFTP
API respectively.
Writing files
In order to store a remote file from a stream of bytes, you need to use the toPath
method in the FTP API:
- Scala
-
protected def storeToPath(path: String, append: Boolean): Sink[ByteString, Future[IOResult]] = Ftp.toPath(path, settings, append)
- Java
-
public Sink<ByteString, CompletionStage<IOResult>> getIOSink(String path) throws Exception { return Ftp.toPath(path, settings()); }
This sink will consume ByteString elements and materializes to Future in Scala API and CompletionStage in Java API of IOResult when the stream finishes.
For both FTPs and SFTP servers, you will need to use the FTPs
and SFTP
API respectively.
Running the example code
The code in this guide is part of runnable tests of this project. You are welcome to browse the code, edit and run it in sbt.
- Scala
-
sbt > ftp/test
- Java
-
sbt > ftp/test