FTP Client
Upload and download files, manage remote folders.
Introduction
Netuno has integrated into its low-code framework an FTP client, which allows connecting to servers, sending and receiving files, modifying folders, among other remote operations.
It supports multiple simultaneous connections, and also FTPS.
This is thanks to the Java and open-source implementation Apache Commons Net, which
allows Netuno to provide these functionalities in all script languages through the _ftp resource.
More about Netuno's FTP resource.
Configuration
The first step is to configure the FTP access credentials 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 whether the FTP configurations are already present; if they do not exist yet, then at the end of the configuration file (in JSON format) add the FTP configuration parameters:
{
...
"ftp": {
"default": {
"enabled": true,
"host": "my.server.net",
"port": 21,
"username": "temp",
"password": "$eCr37"
}
},
...
}
After saving the changes, simply restart Netuno to apply the configuration changes. These are the
default settings for the connection, as indicated by the name default.
You can add more FTP connection settings, but only one can be defined as default.
To add more FTP connection settings, simply repeat the default configuration block and change the name
default to 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 it is invoked as follows in the code:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const ftp = _ftp.init()
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
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const ftpOther = _ftp.init("other")
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
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
ftp.connect()
ftp.connect()
ftp.connect()
ftp.connect()
ftp.connect()
ftp.connect()
Working Directory
When performing operations on FTP, you need to define which folder will be used.
By defining the working directory, we will be able to perform the various manipulations from this base folder.
To change the working directory:
- JavaScript
- TypeScript
- 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")
ftp.changeWorkingDirectory("my/base/folder")
Now any operation will be performed in or from this folder.
Creating and Deleting Folders
To create a folder, simply indicate the path of this folder:
- JavaScript
- TypeScript
- 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")
ftp.createDirectory("temporary-folder")
ftp.createDirectory("temporary-folder/sub-folder")
To delete or remove folders:
- JavaScript
- TypeScript
- 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")
ftp.deleteDirectory("temporary-folder/sub-folder")
ftp.deleteDirectory("temporary-folder")
Rename
It is possible to rename files or folders; the old path is passed followed by the new path:
- JavaScript
- TypeScript
- 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")
ftp.rename("old-name", "new-name")
Uploading Files
To submit a text file:
- JavaScript
- TypeScript
- 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...")
ftp.uploadText("my-file.txt", "Hello FTP! This is my content...")
Receiving Files
To get a text file:
- JavaScript
- TypeScript
- Python
- Ruby
- Kotlin
- Groovy
const fileContent = ftp.downloadText("my-file.txt")
_out.println(fileContent)
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)
Listing Folder Items
Lists all the content of a folder:
- JavaScript
- TypeScript
- 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 (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.