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.3"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-ftp_2.11</artifactId>
  <version>0.3</version>
</dependency>
Gradle
dependencies {
  compile group: "com.lightbend.akka", name: "akka-stream-alpakka-ftp_2.11", version: "0.3"
}

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,
  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.

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(getFileSystem.getPath(path), settings)
Java
public Source<ByteString, CompletionStage<IOResult>> getIOSource(String path) throws Exception {
  return Ftp.fromPath(getFileSystem().getPath(path), settings());
}

This souce 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.

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