In this article, we will learn how to write files to a particular shared drive location using MuleSoft.
Using the SMB connector in Mule, applications have the ability to perform the below operations on Shared Drive.
- Directory create: Create a new directory on the SMB share drive.
- Directory List: List the contents of a directory on the SMB share drive.
- Directory delete: Delete a directory from the SMB share drive.
- File write: Writes data out to a file.
- File read: Reads the contents of a file from the SMB share drive.
- File delete: Delete a file from the SMB share drive.
In our case let us explore the File write to write some file content to the shared drive.
-
Write a File to a Shared Drive.
We will try to write a file in a particular shared drive folder with a file name passed as Query parameter through postman with some file content set in the set payload component.
But before that, we need to import the SMB connector from exchange to Mule Pallete or add the SMB dependency in the SMB file.
<dependency> <groupId>com.mulesoft.connectors</groupId> <artifactId>mule-smb-connector</artifactId> <version>2.1.1</version> <classifier>mule-plugin</classifier> </dependency>
After that, you should be able to find the SMB connector in the Mule Pallete window as shown below.
Drag and drop the HTTP listener from the Mule palette. Then add a variable to set filename here we are getting filename from query parameter. And add a set payload component to set some content that needs to be written to the file. And finally, drag and drop the SMB File write module from the Mule palette.
Create a Global configuration for HTTP Listener and path with the below settings:
Add the set variable with the below value to read filename dynamically during runtime
And set the payload as per your requirement (i,e whatever content you would like to write into a file). In my case, I’m just setting it as “Test file content from Mule”
And then drag and drop the SMB file write module from the Mule palette and add the below-required connector config:
Domain: Enter a Windows domain for the user to log in with
Host: Hostname of server hosting shared drive
Share: Named path of shared drive
Username: enter the username to login
Password: enter the corresponding password
And then make a test connection to make sure the shared drive configuration is valid. On success, it is ready to use.
Also, set the filename field and directory field (to which the file need to be written) with file content as payload as shown below:
Now run this application and hit the request from the postman with the below URL:
http://localhost:8081/writeFile?filename=testFile.txt
You will get the response status as 200(success), then if you go to the specified shared drive directory and search for testFile.txt, you will find the file there.
Howdy! Would you mind if I share your blog
with my facebook group? There’s a lot of folks that I think would really
enjoy your content. Please let me know. Thank you
i am able to create a file using SMB but data not able to see the data in file . it always giving empty file
*its creating a empty file
Hi
The article is good enough to use SMB connector. Can you please share the example values for SMB connector config. I could not able to establish the connection with the server.