Set of utilities helping you to access, download, move or delete files in FTP or sFTP server.
By default when an error is encountered in any of the ftp operations - a critical error is thrown and script terminates the execution. You can set the parameter exitOnError to false to continue executing the script anytime a FTP error is detected.
Download
Copy {% json ftp_params %}
{
"mode": "download",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21,
"fileName": "ftp_file.csv",
"file": "local_file.csv",
"exitOnError": false
}
{% endjson %}
{% assign result = ftp_params | ftp %}
Above snippet downloads remove file called ftp_file.csv
and saves it in your storage as local_file.csv.
Next you can access this file by adding following commands:
Copy {% assign file_content = "local_file.csv" | content %}
{% assign csv_parsed = file_content | parse_csv %}
Your csv_parsed
variable would now contain an array of object with csv columns as property names.
List
Copy {% json ftp_params %}
{
"mode": "list",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21
}
{% endjson %}
{% assign result = ftp_params | ftp %}
Gets all files from remote ftp server. You can next use for
loop to iterate over available files.
Copy {% for file in result.files %}
{{file | log }}
{% endfor %}
Above snippet would log all files on FTP server under directory specified in path
parameter.
Move
Copy {% json ftp_params %}
{
"mode": "move",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21,
"fileName": "ftp_file.csv",
"newPath": "/processed/ftp_file.csv"
}
{% endjson %}
{% assign result = ftp_params | ftp %}
Moves file ftp_file.csv
from /
to /processed/
Delete
Copy {% json ftp_params %}
{
"mode": "delete",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21,
"fileName": "ftp_file.csv"
}
{% endjson %}
{% assign result = ftp_params | ftp %}
Deletes file ftp_file.csv
from /
directory.
Upload
Copy {% capture sample_content %}
{% endjson %}
{% json ftp_params %}
{
"mode": "upload",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21,
"content": {{ sample_content | json }},
"fileName": "sample_upload.txt"
}
{% endjson %}
{% assign result = ftp_params | ftp %}
Uploads file with defined content to ftp. File is saved as sample_upload.txt
sFTP servers
To access sFTP servers additional paramter sftp set to true is expected. To list files on sFTP server following snippet could be used.
Copy {% json sftp_params %}
{
"mode": "list",
"host": "ftp.com",
"user": "user@ftp.com",
"password": "pass123",
"path": "/",
"port": 21,
"sftp": true
}
{% endjson %}
{% assign result = sftp_params | ftp %}
Last updated 5 months ago