FTP Client
Introduction
Netuno integrates an FTP client into its low-code framework, allowing you to connect to servers, send and receive files, modify folders, and perform other remote operations.
It supports multiple simultaneous connections, including FTPS.
This is thanks to its Java and open-source Apache Commons Net
implementation, which allows Netuno to provide these features in all scripting languages through the _ftp
feature.
More about Netuno's FTP feature.
Configuration
The first step will be to configure the FTP access data that Netuno will use to establish the connection.
Navigate within the application you are developing to the environment configuration file. In this example, we will use the development environment file located at:
config/_development.json
Check if it already contains FTP settings. If it doesn't already exist, add the FTP configuration parameters at the end of the configuration file (in JSON format):
{
...
"ftp": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 21,
"username": "temp",
"password": "$eCr37"
}
},
...
}
After saving your changes, simply restart Netuno to apply the configuration changes. These are the default
connection settings, as indicated by the name default
.
You can add more FTP connection settings, but only one can be set as the default.
To add more FTP connection settings, simply repeat the default configuration block and replace the name default
with another name of your choice. See the example:
{
...
"ftp": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 22,
"username": "temp",
"password": "$eCr37"
},
"other": {
"enabled": true,
"host": "my.other-server.net",
"port": 22,
"username": "temp",
"password": "$eCr37"
}
},
...
}
A configuration similar to the example above will allow you to use the default account when called as follows in the code:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const ftp = _ftp.init()
ftp = _ftp.init()
ftp = _ftp.init()
val ftp = _ftp.init()
def ftp = _ftp.init()
And to use the other
configuration, simply add the configuration name as a parameter to the _ftp.init()
function as follows:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const ftpOther = _ftp.init("other")
ftpOther = _ftp.init("other")
ftpOther = _ftp.init("other")
val ftpOther = _ftp.init("other")
def ftpOther = _ftp.init("other")
If you do not add a configuration name as a parameter, the default account will always be used.
Connection
Before doing anything, you need to start the FTP connection:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.connect()
ftp.connect()
ftp.connect()
ftp.connect()
ftp.connect()
Working Folder
When performing FTP operations, you need to define which folder will be used.
By defining the working folder, we can perform various operations from this base folder.
To change the working folder:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.changeWorkingDirectory("my/base/folder")
ftp.changeWorkingDirectory("my/base/folder")
ftp.changeWorkingDirectory("my/base/folder")
ftp.changeWorkingDirectory("my/base/folder")
ftp.changeWorkingDirectory("my/base/folder")
Now any operation will be performed in or from this folder.
Creating and Deleting Folders
When creating a folder, simply specify the path to this folder:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
To delete or remove folders:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
Rename
You can rename files or folders by passing the old path and then the new path:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.rename("old-name", "new-name")
ftp.rename("old-name", "new-name")
ftp.rename("old-name", "new-name")
ftp.rename("old-name", "new-name")
ftp.rename("old-name", "new-name")
Upload Files
To submit a text file:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
Receiving Files
To receive a text file:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
const fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
val fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
def fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
List Items in Folder
Lists all the contents of a folder:
- JavaScript
- Python
- Ruby
- Kotlin
- Groovy
for (const i of ftp.list("path/folder")) {
if (i.isDirectory()) {
_out.print("Folder: ")
} else if (i.isFile()) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(i.getName())
_out.println("<br/>")
}
for i in ftp.list("path/folder"):
if i.isDirectory():
_out.print("Folder: ")
elif i.isFile():
_out.print("File: ")
else:
_out.print("Other: ")
_out.println(i.getName())
_out.println("<br/>")
ftp.list("path/folder").each do |i|
if i.isDirectory()
_out.print("Folder: ")
elsif i.isFile()
_out.print("File: ")
else
_out.print("Other: ")
end
_out.println(i.getName())
_out.println("<br/>")
end
ftp.list("path/folder").forEach {
if (it.isDirectory()) {
_out.print("Folder: ")
} else if (it.isFile()) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(it.getName())
_out.println("<br/>")
}
for (i in ftp.list("path/folder")) {
if (i.isDirectory()) {
_out.print("Folder: ")
} else if (i.isFile()) {
_out.print("File: ")
} else {
_out.print("Other: ")
}
_out.println(i.getName())
_out.println("<br/>")
}
Returns a list of objects of type FTPFile.