# FTP

Set up the connection once, then add a table for each file or group of files you want to load.

**Before you start**

You need:

- **The host name, port and connection type** (FTP, FTPS or SFTP).
- **A user that can read the files,** with its password, or a private key for SFTP.
- **Keboola's IP addresses allowed,** if the server accepts connections only from known addresses. See IP addresses (/components/ip-addresses/).
- **The path or glob pattern** of the files you want.

What each tab needs:

- **Prompt tab:** Kai, the **Kai Agent** button in the top bar, on supported stacks. A new Free Plan project has it from the start; elsewhere an organization admin turns it on once: Get started with Kai (/kai/getting-started/).
- **UI tab:** a browser.
- **CLI / API tab:** kbagent (/cli/getting-started/), connected to your project. The checks only read.

## Configuration
[Create a new configuration](https://help.keboola.com/components/configurations/#create-a-configuration) of the **FTP** connector.
To access the files on FTP, you need to set up access credentials.

![Screenshot - FTP extractor server configuration](https://help.keboola.com/components/extractors/storage/ftp/ftp-1.png)

You can use a private key for the **SFTP** connection type. If you fill in the private key,
the connector will use it for login instead of the password. You can also change
the port for each connection method. The default port for **FTP** and **FTPS** is **21**, and for **SFTP**, it is **22**.

## Add Tables
To create a new table, click the **Add Table** button and assign a name. It will
be used to create the destination table name in Storage and can be modified.

![Screenshot - FTP extractor create table](https://help.keboola.com/components/extractors/storage/ftp/ftp-2.png)

Configured tables are stored as [configuration rows](https://help.keboola.com/components/#configuration-rows).
Each table has a different setting, but all tables share the same credentials to the server.

### Specify Files to Download
For each table, you have to specify a path. Use the path to a specific file, or
use the glob syntax (for testing the glob path, you can use the [Glob tester](https://www.digitalocean.com/community/tools/glob)).
If you want to list inside subdirectories (e.g., `/dir/**/*.csv`), please make sure that your FTP server
supports a recursive list of directories.

![Screenshot - FTP extractor download settings](https://help.keboola.com/components/extractors/storage/ftp/ftp-3.png)

- **New Files Only**: The connector will keep track of downloaded files and will continue with the unprocessed files
 on the next run. To reset the state which keeps track of the progress and enables to continue with new files, 
 use the **Reset State** button or uncheck the **New Files Only** option and run the connector again.
 - **Decompress**: All downloaded files will be decompressed (currently supporting ZIP and GZIP). All files in all archives
 will be imported into a single Storage table.

### Save Settings
Now determine how to save the data in Storage.

![Screenshot - FTP extractor save settings](https://help.keboola.com/components/extractors/storage/ftp/ftp-4.png)

- The initial value in **Table Name** is derived from the configuration table name. You can change it at any time; however,
the [Storage bucket](https://help.keboola.com/storage/buckets/) where the table will be saved cannot be changed.
- **Incremental Load** will turn on [incremental loading to Storage](https://help.keboola.com/storage/tables/incremental-loading/#incremental-loading). The result of the
incremental load depends on other settings (mainly **Primary Key**).
- **Delimiter** and **Enclosure** specify the CSV settings.

### Header & Primary Key
There are three options for determining column names:

![Screenshot - FTP extractor header & primary key](https://help.keboola.com/components/extractors/storage/ftp/ftp-5.png)

- **Set header manually** -- enables the Set Header input to manually specify all columns in the table.
- **Read from the file header** -- assumes that each file has a header in the first row. A random file will be chosen to extract the header, and the first row in all files will be removed.
- **Generate automatically** -- names the columns sequentially as col_1, col_2, and so on.

**Primary Key** can be used to specify the primary key in Storage. It can be used with **Incremental Load**
and **New Files Only** to create a configuration that incrementally loads all new files into a table in Storage.

### Audit
The data source connector can optionally add audit columns to the table. `ftp_filename` adds the processed file name, and `ftp_row_number`
adds the row number in the source file.

![Screenshot - FTP extractor audit](https://help.keboola.com/components/extractors/storage/ftp/ftp-6.png)

Save the table and click **Run Component**.

**Check:**

#### Prompt

```text title="Prompt" wrap
Did the latest job of my FTP configuration succeed, and which tables did it load, with row counts?
```

Kai only reads here, so it asks for no approval, and its answer links to what it found in your project.

#### UI

The job ends with status **Success** and Storage has a table with the **Table Name** you set, with the
rows of the matched files.

#### CLI / API

```bash
kbagent job list --component-id keboola.ex-ftp --config-id 123456789 --limit 1
kbagent storage tables --bucket-id in.c-keboola-ex-ftp-123456789
```

Replace `123456789` with your configuration ID: `status` shows whether the job succeeded, and `id` and `rows_count` list the tables it loaded.

---

The connector also supports [Debug mode](https://help.keboola.com/components/debug-mode/), all supported
parameters are described in the [GitHub repository](https://github.com/keboola/ex-ftp).
