File Connector Example¶
File Connector can be used to perform operations in the local file system as well as in a remote server such as FTP and SFTP.
What you'll build¶
This example explains how to use File Connector to create a file in the local file system and read the particular file. The user sends a payload specifying which content is to be written to the file. Based on that content, a file is created in the specified location. Then the content of the file can be read as an HTTP response by invoking the other API resource upon the existence of the file. Similarly, the same example can be configured to communicate with a remote file system (i.e FTP server) easily. The example also uses some other WSO2 mediators to manipulate messages.
It will have two HTTP API resources, which are create
and read
.
-
/create
: The user sends the request payload which includes the location where the file needs to be saved and the content needs to be added to the file. This request is sent to the integration runtime by invoking the FileConnector API. It saves the file in the specified location with the relevant content. -
/read
: The user sends the request payload, which includes the location of the file that needs to be read. This request is sent to the integration runtime where the FileConnector API resides. Once the API is invoked, it first checks if the file exists in the specified location. If it exists, the content is read and response is sent to the user. If the file does not exist, it sends an error response to the user.
If you do not want to configure this yourself, you can simply get the project and run it.
Set up the integration project¶
Follow these steps to set up the Integration Project using the WSO2 Micro Integrator Visual Studio Code extension.
Create the new project¶
-
Go to WSO2 Micro Integrator in the VS Code.
-
Click on Create New Project to create the new integration project.
-
provide the Project Name and select the Project Directory
-
You have now created the new project with the following structure.
Create the integration logic¶
Create the first resource¶
-
Click on the + button next to the APIs and provide the API name as
FileConnector
and the API context as/fileConnector
. -
To create the
/create
resource. Click on Edit and update the value ofURI template
to/create
and value ofmethod
toPost
. -
Open the API in the diagram view and click on the + button below to START to add connectors or mediators.
-
To add the write operation under the API, click on Connectors, select the file connector, and choose the write operation.
-
Click on Add new connection to create the new local file connection.
-
After creating the local file connection, update the values for file path and Content for the write operation.
Source view of the implemented resource
<resource methods="POST" uri-template="/create">
<inSequence>
<file.write configKey="local_file_connection">
<filePath>{json-eval($.filePath)}</filePath>
<contentOrExpression>{json-eval($.inputContent)}</contentOrExpression>
<mimeType>Automatic</mimeType>
<writeMode>Overwrite</writeMode>
<appendPosition>0</appendPosition>
<encoding>UTF-8</encoding>
<compress>false</compress>
<appendNewLine>false</appendNewLine>
<enableStreaming>false</enableStreaming>
<enableLock>false</enableLock>
<updateLastModified>true</updateLastModified>
<includeResultTo>Message Body</includeResultTo>
</file.write>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
Create the second resource¶
-
Click on + Resource to add new resource.
-
Use the URL template as
/read
. The method will be POST. -
Implement the following API as described above.
Source view
<resource methods="POST" uri-template="/read">
<inSequence>
<property name="path" expression="json-eval($.filePath)"/>
<file.checkExist configKey="local_file_connection">
<path>{$ctx:path}</path>
<includeResultTo>Message Body</includeResultTo>
</file.checkExist>
<switch source="json-eval($.checkExistResult.fileExists)">
<case regex="true">
<file.read configKey="local_file_connection">
<path>{$ctx:path}</path>
<readMode>Complete File</readMode>
<startLineNum>0</startLineNum>
<endLineNum>0</endLineNum>
<lineNum>0</lineNum>
<encoding>UTF-8</encoding>
<enableStreaming>false</enableStreaming>
<enableLock>false</enableLock>
<includeResultTo>Message Body</includeResultTo>
</file.read>
<respond/>
</case>
<default>
<log category="INFO" level="simple">
<property name="Status" value="File does not exist"/>
</log>
<drop/>
</default>
</switch>
</inSequence>
<faultSequence>
</faultSequence>
</resource>
Get the project¶
You can download the ZIP file and extract the contents to get the project code.
Deployment¶
Once you have built your artifacts into a composite application, you can export it into a CAR file (.car file) using the WSO2 Micro Integrator Visual Studio Code extension:
-
Open the project overview and click on Export.
-
Click on Select Destination to select a destination folder to export the CAR file.
Test¶
File create operation¶
Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.
curl --location 'http://localhost:8290/fileConnector/write' \
--header 'Content-Type: application/json' \
--data '{
"filePath":"/Users/wso2/Documents/mi_testing/doc/create.txt",
"inputContent": "This is a test file"
}'
Expected response:
You should get a 'Success' response, and the file should be created in the specified location in the above payload.
File read operation¶
Invoke the API as shown below using the curl command. Curl Application can be downloaded from here.
curl --location 'http://localhost:8290/fileConnector/read' \
--header 'Content-Type: application/json' \
--data '{
"filePath":"/Users/wso2/Documents/mi_testing/doc/create.txt"
}'
Expected response:
You should get the following text returned.
This is a test file.
What's next¶
- To customize this example for your own scenario, see File Connector Reference Guide documentation for all operation details of the connector.