Skip to main content

SMB

SMB file integrations poll a directory on a network share and process files as they arrive. Use them when the files you need already live on a Windows file server, a NAS appliance, or a Samba host, rather than on an FTP endpoint.

The listener speaks SMB dialects 2.0.2 through 3.1.1 and authenticates with NTLMv2 credentials or Kerberos. It can optionally require message signing and encrypt data in transit.

Creating an SMB service​

  1. Select + Add Artifact in the canvas, or select + next to Entry Points in the sidebar.

  2. In the Artifacts panel, select SMB under File Integration.

    Artifacts panel showing SMB under File IntegrationArtifacts panel showing SMB under File Integration
  3. In the Create SMB Integration form, keep Create new selected to define a listener. Choose Use existing instead to attach the service to a listener the project already has.

    Create SMB Integration form with username and password authenticationCreate SMB Integration form with username and password authentication
  4. Fill in the Listener Configuration:

    FieldDescription
    Listener NameIdentifier for this listener (e.g., smbListener).
    HostHostname or IP address of the SMB server (e.g., fileserver.example.com).
    PortTCP port of the SMB service. Defaults to 445.
    ShareName of the share to connect to (e.g., reports). Every path is resolved inside this share.
  5. Choose an authentication method:

    OptionFields revealedUse when
    No Authentication—The share allows guest access.
    Username / PasswordUsername, Password, DomainNTLMv2 — the usual choice for Windows and Samba. Leave Domain at WORKGROUP for standalone servers, or set the Active Directory domain.
    KerberosPrincipal and keytab settingsThe domain requires mutual authentication.
  6. Enter the Monitored Path — the directory inside the share to poll (e.g., /incoming). Defaults to /.

  7. Select Create. WSO2 Integrator opens the service with its listener and monitored path shown at the top.

    Service designer for a new SMB service before any handler is addedService designer for a new SMB service before any handler is added
  8. Select + Handler to define how incoming files are processed.

Tip: Bind Username and Password to configurable variables instead of typing literals. Switch the field to Expression and reference a configurable so credentials never reach source control.

File handlers​

A file handler is a remote function the runtime calls each time a polling cycle detects a file event in the monitored directory. A service can declare any combination of the three handler kinds:

HandlerTrigger
On Create (onFileText / onFileJson / onFileXml / onFileCsv / onFile)A new file appears in the monitored directory. The function name depends on the content format — one variant per format.
On Delete (onFileDelete)A previously seen file is no longer present on the share.
On Error (onError)The runtime could not map incoming content to a typed handler — for example, a JSON handler received malformed JSON.

Adding a file handler​

In the service designer, select + Handler, then pick On Create, On Delete, or On Error. The handler configuration panel opens on the right.

Configure On Create Handler panel with format, file handling options, and advanced parametersConfigure On Create Handler panel with format, file handling options, and advanced parameters
FieldDescription
Format(On Create only) The format of incoming files. Determines the handler function name and the type of the content parameter. Options: Text, JSON, XML, CSV, Raw Bytes. See Content types.
+ Define Content Schema(JSON, XML only) Binds the document to a record type you build in a form. See Data binding.
+ Define Row Schema(CSV only) Binds each row to a record type you build in a form. See Data binding.
Stream (Large Files)(CSV, Raw Bytes only) Hands the handler the file a piece at a time instead of loading it all into memory. See Streaming large files.
On SuccessAction to take when the handler completes without returning an error: Move the file to another directory, or Delete it. See Post-processing.
On ErrorAction to take when the handler returns an error. Same choices as On Success.
Move ToDestination directory, required when Move is selected.

Expand Advanced Parameters for optional handler parameters:

FieldDescription
File Metadata (fileInfo)Adds an smb:FileInfo parameter carrying the file's name, path, size, and timestamps. See FileInfo.
SMB Connection (caller)Adds an smb:Caller parameter for read and write operations on the same share. See Caller operations.

Select Save to add the handler. It appears in the Event Handlers list, tagged with its kind.

Event Handlers list showing the saved onCreate handlerEvent Handlers list showing the saved onCreate handler

Select the handler to implement its body in the flow designer.

Flow designer for the onFileText handlerFlow designer for the onFileText handler

Content types​

The Format chosen on an On Create handler determines the function name and the type of the content parameter.

FormatHandler functionContent typeUse when
TextonFileTextstringFiles are plain text (logs, EDI, custom formats).
JSONonFileJsonjson or a record typeFiles are JSON documents.
XMLonFileXmlxml or a record typeFiles are XML documents.
CSVonFileCsvstring[][], a record array, or a stream variantFiles are comma-separated values. Define a row schema to bind each row to a record type.
Raw BytesonFilebyte[] or stream<byte[], error?>Binary files, or when you need raw byte access.

Post-processing: moving or deleting files​

Once a handler finishes — successfully or with an error — the runtime can move the file elsewhere on the same share or delete it. Configure this on the handler form; switch to the Ballerina Code tab only to review the generated annotation.

The File Handling Options section has two independent toggles:

EventTicked by default?Action pickerExtra input
On SuccessYesMove or DeleteMove To destination, required when Move is chosen
On ErrorYesMove or DeleteMove To destination, required when Move is chosen

Common combinations:

  • Move on success, move on error — archive processed files and quarantine failures in separate directories.
  • Delete on success, move on error — discard files that processed cleanly, keep failures for review.
  • Leave the file in place — clear On Success or On Error to skip the action for that outcome.

Data binding​

A handler can take the file content in a generic shape (json, xml, or a list of raw CSV values), or bound to a record type you define. Binding to a record type is what lets you refer to order.quantity in the handler instead of picking values out of a raw document, and it catches misspelled fields and wrong value types before the integration ever runs.

New to record types? A record type is just a named shape for your data — a list of the fields it holds and the kind of value in each. If your CSV has the columns orderId, product, and quantity, the record type names those three fields and says which holds text and which holds a number. You build it from a form in the Visual Designer, so there is nothing to write by hand. For the language-level detail, see Type System & Records.

On the Add File Handler form, the Format you pick decides which button appears:

FormatButtonWhat it does
JSON+ Define Content SchemaBuilds a record type for the whole JSON document. Skip it and the handler receives plain json.
XML+ Define Content SchemaBuilds a record type for the whole XML document. Skip it and the handler receives plain xml.
CSV+ Define Row SchemaBuilds a record type for one row; each row of the file then arrives as one filled-in record. Skip it and every row arrives as a list of raw text values.
Text, Raw Bytes—Nothing to define — the content is always plain text or raw bytes.

The button opens a record builder. Add one field at a time, giving each a name and a type:

Pick this typeFor values like
stringText — names, IDs, reference codes
intWhole numbers — quantities, counts
decimalNumbers with a fractional part — prices, rates
booleanTrue/false flags
Another recordA nested object inside the document

Field names have to match what is in the file — the JSON keys, the XML element names, or the CSV column headers. Once you save the schema the handler is rewritten to use it, and the fields become available by name in the expression editor everywhere you use the content.

Streaming large files​

The runtime normally reads the whole file into memory before calling the handler. For a large file — a multi-gigabyte CSV export, say — that can exhaust the memory the integration has to work with. Streaming avoids it: the runtime hands the handler the file a piece at a time and the handler works through the pieces in order.

Tick Stream (Large Files) on the Add File Handler form. The option only appears for the CSV and Raw Bytes formats:

FormatWhat the handler receives when streaming is on
CSVOne row at a time. Combine it with + Define Row Schema and each row arrives as a filled-in record.
Raw BytesThe file in 64 KB blocks.

JSON and XML offer no streaming option — both have to be read in full before they can be parsed.

Two things to keep in mind:

  • Leave the option off unless the files really are large. A handler that gets the whole content is simpler to build.
  • Streamed content can only be read once, start to finish. If the handler needs to go over the content twice, or look at the end before the beginning, don't stream it.
When a row fails to parse

A bad row (malformed CSV or wrong type) stops the stream right there. The handler returns an error, so the file takes the On Error path you configured under Post-processing. Anything your handler already did for earlier rows (database writes, API calls, published messages) stays.

When you retry the file, those rows run again. To stay safe, make your handler idempotent (check before you write) or track which rows you've already processed per file. To skip malformed rows instead of failing the entire file, enable CSV fault tolerance.

FileInfo​

Enable File Metadata (fileInfo) to receive an smb:FileInfo parameter describing the incoming file.

FieldTypeDescription
namestringFile name without path
pathstringPath within the share
sizeintFile size in bytes
modifiedAttime:UtcLast-modified time
createdAttime:UtcCreation time
accessedAttime:UtcLast-accessed time
writtenAttime:UtcLast-written time
isDirectorybooleantrue if the entry is a directory
extensionstringFile extension
isExecutablebooleantrue if the file is marked executable
isHiddenbooleantrue if the file is marked hidden
isWritablebooleantrue if the file is writable
uristringFull URI of the file

Caller operations​

For most cases the content parameter and the post-processing annotation are enough. When you need more control — reading a related file, writing output elsewhere, or managing files yourself — enable SMB Connection (caller) to add an smb:Caller parameter. It reuses the listener's session.

Reading files:

OperationReturn typeDescription
caller->getText(path)string|ErrorRead a file as plain text
caller->getBytes(path)byte[]|ErrorRead a file as a byte array
caller->getJson(path)json|ErrorRead and parse JSON
caller->getXml(path)xml|ErrorRead and parse XML
caller->getCsv(path)string[][]|ErrorRead and parse CSV
caller->getBytesAsStream(path)stream<byte[], error?>|ErrorRead as a byte stream for large files
caller->getCsvAsStream(path)stream<string[], error?>|ErrorRead CSV as a stream for large files

Writing files:

Write operations take an smb:FileWriteOption — smb:OVERWRITE (default) or smb:APPEND.

OperationDescription
caller->putText(path, content, option)Write a string to a file
caller->putBytes(path, content, option)Write a byte array to a file
caller->putJson(path, content, option)Serialize and write JSON
caller->putXml(path, content, option)Serialize and write XML
caller->putCsv(path, content, option)Write CSV from string[][] or a record array
caller->putBytesAsStream(path, stream, option)Write a byte stream to a file
caller->putCsvAsStream(path, stream, option)Write CSV from a stream
caller->patch(path, content, offset)Overwrite part of a file at a byte offset

File management:

OperationReturn typeDescription
caller->list(path)FileInfo[]|ErrorList files and directories
caller->mkdir(path)Error?Create a directory
caller->rmdir(path)Error?Remove a directory
caller->rename(origin, destination)Error?Rename a file or directory
caller->move(source, destination)Error?Move a file
caller->copy(source, destination)Error?Copy a file
caller->exists(path)boolean|ErrorCheck whether a path exists
caller->size(path)int|ErrorGet a file's size in bytes
caller->isDirectory(path)boolean|ErrorCheck whether a path is a directory
caller->delete(path)Error?Delete a file

Service configuration​

The @smb:ServiceConfig annotation controls what the service monitors.

In the service designer, select Configure to edit the service's settings. The Monitored Path shown beside the listener name at the top of the designer reflects the annotation's path field.

@smb:ServiceConfig fields:

FieldTypeDefaultDescription
pathstring?Service nameDirectory inside the share to monitor. Paths are relative to the share root.

Listener configuration​

The listener controls how to connect — host, share, authentication, dialects, and polling behaviour. One listener represents one connection to one share. Open its configuration by selecting the listener name (for example, smbListener) under Listeners in the sidebar.

FieldDescriptionDefault
HostHostname or IP address of the SMB server.localhost
PortTCP port of the SMB service.445
ShareName of the share to connect to. Required.—
AuthNTLMv2 credentials or a Kerberos configuration.—
File Name PatternRegex a file name must match to trigger any handler.—
Polling IntervalSeconds between directory polls.60
DialectsSMB protocol dialects to negotiate, in order of preference.All dialects
Sign RequiredWhether SMB message signing is required.false
Encrypt DataWhether to encrypt data in transit.false
Enable DFSWhether to follow Distributed File System referrals.false
Buffer SizeRead and write buffer size in bytes.65536
Connect TimeoutConnection timeout in seconds.30.0
Lax Data BindingWhen true, data-binding errors yield () instead of surfacing as an error.false
CSV Fail SafeSkip malformed CSV rows rather than rejecting the whole file.—

What's next​

  • CSV fault tolerance — skip malformed rows instead of failing the whole file
  • FTP / SFTP — poll a remote FTP, SFTP, or FTPS server instead of a network share
  • Local files — monitor a local directory
  • SMB connector — call SMB operations from an integration instead of being triggered by them
  • Data Mapper — transform incoming file payloads between formats