Skip to main content

Actions

The SMB connector exposes the following clients:

Available clients:

ClientPurpose
ClientPerform imperative file operations on SMB shares — read, write, patch, list, create, delete, move, copy, and rename files and directories

For event-driven integration, see the Trigger Reference.


Client​

Connects to an SMB share to read, write, and manage files and directories, with support for text, JSON, XML, CSV, and binary content.

Configuration​

ClientConfiguration

FieldTypeDefaultDescription
hoststring"localhost"Target SMB server hostname or IP address
portint445Port number of the SMB service
sharestringRequiredSMB share name to connect to
authAuthConfiguration—Authentication credentials for the SMB connection
dialectsDialect[][SMB_3_1_1, SMB_3_0_2, SMB_3_0, SMB_2_1, SMB_2_0_2]SMB protocol dialects to negotiate with, in order of preference
signRequiredbooleanfalseWhether SMB message signing is required
encryptDatabooleanfalseWhether to encrypt SMB data
enableDfsbooleanfalseWhether to enable Distributed File System (DFS) support
bufferSizeint65536Size of the buffer for read/write operations in bytes
connectTimeoutdecimal30.0Connection timeout in seconds
laxDataBindingbooleanfalseWhether to relax data binding for XML, JSON, and CSV content
csvFailSafeFailSafeOptions—When set, skips malformed CSV records and logs them to a file instead of failing the operation

AuthConfiguration

FieldTypeDefaultDescription
credentialsCredentials—NTLMv2 credentials for the SMB connection
kerberosConfigKerberosConfig—Kerberos authentication configuration

Credentials

FieldTypeDefaultDescription
usernamestringRequiredUsername for SMB authentication
passwordstringRequiredPassword for SMB authentication
domainstring"WORKGROUP"Domain for domain-based authentication

KerberosConfig

FieldTypeDefaultDescription
principalstringRequiredKerberos principal name in the user@REALM format
keytabstring—Path to the keytab file; the password is used when this is not provided
configFilestring—Path to the Kerberos configuration file (krb5.conf)

FailSafeOptions

FieldTypeDefaultDescription
contentTypeErrorLogContentTypeMETADATAWhat to record in the error log for each skipped CSV record: METADATA, RAW, or RAW_AND_METADATA

Initializing the client​

import ballerina/smb;

smb:ClientConfiguration config = {
host: "smb-server.example.com",
share: "myshare",
auth: {
credentials: {
username: "smbuser",
password: "smbpassword",
domain: "WORKGROUP"
}
}
};
smb:Client client = check new (config);

Operations​

Read Operations​

getBytes

Reads a file from an SMB share as a byte array.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: byte[]|Error

Sample code:

byte[] content = check client->getBytes("/files/image.png");

Sample response:

[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100]
getText

Reads a file from an SMB share as text.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: string|Error

Sample code:

string content = check client->getText("/files/readme.txt");

Sample response:

Hello, World!
This is file content read from the SMB share.
getJson

Reads a file from an SMB share and parses it as JSON. Supports type binding to records and other Ballerina types via the targetType parameter.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
targetTypetypedesc<json>NoThe type descriptor of the target type; defaults to json

Returns: json|Error

Sample code:

// Read as generic json
json content = check client->getJson("/data/config.json");

// Read with type binding
type Config record {| string env; int maxRetries; |};
Config config = check client->getJson("/data/config.json");

Sample response:

{
"env": "production",
"maxRetries": 3
}
getXml

Reads a file from an SMB share and parses it as XML. Supports type binding to records and other Ballerina types via the targetType parameter.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
targetTypetypedesc<xml|record {}>NoThe type descriptor of the target type; defaults to xml

Returns: xml|Error

Sample code:

// Read as generic xml
xml content = check client->getXml("/data/report.xml");

// Read with type binding
type Book record {| string title; string author; |};
Book book = check client->getXml("/data/book.xml");

Sample response:

<report><title>Monthly Sales</title><total>42500</total></report>
getCsv

Reads a file from an SMB share and parses it as CSV. Supports parsing to string arrays or record arrays with type binding via the targetType parameter.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
targetTypetypedesc<string[][]|record {}[]>NoThe type descriptor of the target type; defaults to string[][]

Returns: string[][]|Error

Sample code:

// Read as raw string arrays
string[][] rows = check client->getCsv("/data/employees.csv");

// Read with type binding
type Employee record {| string name; int age; string city; |};
Employee[] employees = check client->getCsv("/data/employees.csv");

Sample response:

[
["name", "age", "city"],
["Alice", "30", "New York"],
["Bob", "25", "London"]
]
getBytesAsStream

Retrieves the file content as a byte stream from an SMB share. Use this for large files to avoid loading the entire content into memory.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path to the file on the SMB server

Returns: stream<byte[], error?>|Error

Sample code:

stream<byte[], error?> byteStream = check client->getBytesAsStream("/files/large-video.mp4");
record {| byte[] value; |}? chunk = check byteStream.next();

Sample response:

[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
getCsvAsStream

Retrieves the file content as a CSV stream from an SMB share. Supports streams of string arrays or bound record types, suitable for large CSV files processed row by row.

Parameters:

NameTypeRequiredDescription
pathstringYesThe path to the file on the SMB server
targetTypetypedesc<string[]|record {}>NoExpected element type for automatic data binding; defaults to string[]

Returns: stream<string[], error?>|Error

Sample code:

// Stream as string arrays
stream<string[], error?> csvStream = check client->getCsvAsStream("/data/large-data.csv");
record {| string[] value; |}? row = check csvStream.next();

// Stream with type binding
type Product record {| string id; string name; decimal price; |};
stream<Product, error?> productStream = check client->getCsvAsStream("/data/products.csv");

Sample response:

["id", "name", "price"]

Write Operations​

putBytes

Writes byte array content to a file on an SMB share. Use OVERWRITE to replace any existing file or APPEND to add to it.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentbyte[]YesByte array content to write
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

byte[] data = [72, 101, 108, 108, 111];
smb:Error? result = check client->putBytes("/uploads/binary.dat", data, smb:OVERWRITE);
putText

Writes text content to a file on an SMB share. Use OVERWRITE to replace any existing file or APPEND to add to it.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentstringYesText content to write
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

smb:Error? result = check client->putText("/logs/app.log", "Application started\n", smb:APPEND);
putJson

Writes JSON content to a file on an SMB share. Use OVERWRITE to replace any existing file or APPEND to add to it.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentjsonYesJSON content to write
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

json data = {name: "John", age: 30};
smb:Error? result = check client->putJson("/data/user.json", data, smb:OVERWRITE);
putXml

Writes XML content to a file on an SMB share. Accepts either a native xml value or a record that is automatically converted to XML. Use OVERWRITE to replace any existing file or APPEND to add to it.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentxml|record {|json...;|}YesXML content or a record to be serialized as XML
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

xml data = xml `<person><name>John</name></person>`;
smb:Error? result = check client->putXml("/data/person.xml", data, smb:OVERWRITE);
putCsv

Writes CSV content to a file on an SMB share. Accepts both array-of-arrays (string[][]) and array-of-records formats. When overwriting with a record array, column headers are added automatically.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentstring[][]|record {}[]YesCSV content as an array of string arrays or array of records
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

string[][] csvData = [["name", "age"], ["Alice", "30"], ["Bob", "25"]];
smb:Error? result = check client->putCsv("/reports/employees.csv", csvData, smb:OVERWRITE);
putBytesAsStream

Writes a byte stream to a file on an SMB share. Use this to stream large files without loading the entire content into memory.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentstream<byte[], error?>YesByte stream content to write
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

stream<byte[], error?> byteStream = check client->getBytesAsStream("/source/video.mp4");
smb:Error? result = check client->putBytesAsStream("/dest/video.mp4", byteStream, smb:OVERWRITE);
putCsvAsStream

Writes a CSV stream to a file on an SMB share. Accepts streams of string arrays or records, suitable for large CSV datasets processed row by row.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentstream<string[]|record {}, error?>YesCSV stream content as a stream of string arrays or records
optionFileWriteOptionNoFile write option: OVERWRITE or APPEND (default: OVERWRITE)

Returns: Error?

Sample code:

stream<string[], error?> csvStream = check client->getCsvAsStream("/source/data.csv");
smb:Error? result = check client->putCsvAsStream("/dest/data.csv", csvStream, smb:OVERWRITE);
patch

Writes byte array content at a specified byte offset in a file on an SMB share, leaving the rest of the file untouched.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path
contentbyte[]YesByte array content to write
offsetintYesThe byte offset position in the file where writing should start

Returns: Error?

Sample code:

byte[] patch = [65, 66, 67];
smb:Error? result = check client->patch("/files/data.bin", patch, 100);

Directory Operations​

list

Lists files and directories in a folder on an SMB share, returning metadata for each entry.

Parameters:

NameTypeRequiredDescription
pathstringYesThe directory path

Returns: FileInfo[]|Error

Sample code:

smb:FileInfo[] entries = check client->list("/reports");

Sample response:

[
{
"name": "report-2024.json",
"path": "/reports/report-2024.json",
"size": 2048,
"modifiedAt": [1706745600, 0.0],
"createdAt": [1706745600, 0.0],
"accessedAt": [1706745600, 0.0],
"writtenAt": [1706745600, 0.0],
"isDirectory": false,
"extension": "json",
"isExecutable": false,
"isHidden": false,
"isWritable": true,
"uri": "smb://smb-server/myshare/reports/report-2024.json"
}
]
mkdir

Creates a new directory on an SMB share.

Parameters:

NameTypeRequiredDescription
pathstringYesThe directory path to create

Returns: Error?

Sample code:

smb:Error? result = check client->mkdir("/archive/2024");
rmdir

Deletes an empty directory on an SMB share. The directory must be empty before it can be removed.

Parameters:

NameTypeRequiredDescription
pathstringYesThe directory path to delete

Returns: Error?

Sample code:

smb:Error? result = check client->rmdir("/archive/2024");

File Management​

rename

Renames a file or directory on an SMB share, or moves it to a new location within the same share.

Parameters:

NameTypeRequiredDescription
originstringYesThe source file or directory location
destinationstringYesThe destination file or directory location

Returns: Error?

Sample code:

smb:Error? result = check client->rename("/files/old-name.txt", "/files/new-name.txt");
move

Moves a file from one location to another on an SMB share.

Parameters:

NameTypeRequiredDescription
sourcePathstringYesThe source file location
destinationPathstringYesThe destination file location

Returns: Error?

Sample code:

smb:Error? result = check client->move("/inbox/report.json", "/processed/report.json");
copy

Copies a file from one location to another on an SMB share, leaving the original file in place.

Parameters:

NameTypeRequiredDescription
sourcePathstringYesThe source file location
destinationPathstringYesThe destination file location

Returns: Error?

Sample code:

smb:Error? result = check client->copy("/templates/invoice.xml", "/output/invoice-2024.xml");
delete

Deletes a file from an SMB share.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: Error?

Sample code:

smb:Error? result = check client->delete("/temp/scratch.txt");
exists

Checks if a file or directory exists on an SMB share.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: boolean|Error

Sample code:

boolean fileExists = check client->exists("/uploads/report.json");

Sample response:

true
size

Gets the size of a file on an SMB share in bytes.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: int|Error

Sample code:

int fileSize = check client->size("/uploads/data.csv");

Sample response:

204800
isDirectory

Checks if a given resource on an SMB share is a directory.

Parameters:

NameTypeRequiredDescription
pathstringYesThe resource path

Returns: boolean|Error

Sample code:

boolean isDir = check client->isDirectory("/archive");

Sample response:

true

Connection​

close

Closes the SMB client connection and releases associated resources.

Parameters:

None

Returns: Error?

Sample code:

smb:Error? result = check client->close();