Class SftpFileSystem


public class SftpFileSystem extends FileSystem
Class for accessing the SFTP file system.
  • Constructor Details

    • SftpFileSystem

      public SftpFileSystem(String userName, String password)
      Connects to the specified SFTP server at localhost, default port 22.
      Parameters:
      userName - name of the user
      password - password of the user
    • SftpFileSystem

      public SftpFileSystem(String hostName, int port, String userName, String password)
      Connects to the specified SFTP server.
      Parameters:
      hostName - hostname of the SFTP server
      port - the port to connect to
      userName - name of the user
      password - password of the user
    • SftpFileSystem

      public SftpFileSystem(String hostName, int port)
      Connects to the specified SFTP server with credentials supplied through a CredentialsResolver (see setCredentialsResolver(CredentialsResolver)).
      Parameters:
      hostName - hostname of the SFTP server
      port - the port to connect to
  • Method Details

    • setCredentialsResolver

      public SftpFileSystem setCredentialsResolver(CredentialsResolver credentialsResolver)
      Sets the resolver supplying this file system's user name and password (Credentials.USERNAME/Credentials.PASSWORD) — resolved on each open(), never at construction, so rotated secrets are picked up on the next connection. Keys absent from the resolved credentials fall back to the constructor-supplied values.
      Overrides:
      setCredentialsResolver in class FileSystem
    • open

      public void open() throws DataException
      Opens a connection to an SFTP server.
      Overrides:
      open in class Endpoint
      Throws:
      DataException
    • close

      public void close() throws DataException
      Closes the connection from an SFTP server.
      Overrides:
      close in class FileSystem
      Throws:
      DataException
    • readFile

      public InputStream readFile(String folderName, String fileName) throws Throwable
      Reads a file in the specified folder. To specify a nested folder use "/" as separator.
      Example:
      • readFile("folderOne", "fileName") - reads the file in folderOne which is under the root folder
      • readFile("folderOne/folderTwo", "fileName") - reads the file in folderTwo which is under folderOne
      Returns:
      an InputStream or null
      Throws:
      Throwable
    • readFile

      public InputStream readFile(String fileName) throws Throwable
      Reads the specified file in the root folder.
      Returns:
      an InputStream or null
      Throws:
      Throwable
    • listFolder

      public List<SftpClient.DirEntry> listFolder(String folderName) throws Throwable
      Returns the contents of the folder. To specify a nested folder use "/" as separator.
      Example:
      • listFolder("folderOne") - reads the contents of folderOne which is under the root folder
      • listFolder("folderOne/folderTwo") - reads the contents of folderTwo which is under folderOne
      Throws:
      Throwable
    • listRootFolder

      public List<SftpClient.DirEntry> listRootFolder() throws Throwable
      Returns the contents of the root folder.
      Throws:
      Throwable
    • writeFile

      public OutputStream writeFile(String fileName) throws Throwable
      Writes a file to the root folder. If there is a file with the same name, it is overwritten.
      Parameters:
      fileName - the name of the file
      Throws:
      Throwable
    • writeFile

      public OutputStream writeFile(String folderName, String fileName) throws Throwable
      Writes a file to the specified folder. If there is a file with the same name, it is overwritten.
      Parameters:
      folderName - the folder name
      fileName - the name of the file
      Throws:
      Throwable