Skip to main content

File and folder mapping

Use a file or folder mapping entry to publish a file or a folder of the local file system through OPC UA. The entry connects a node of the address space to the file or to the folder. An OPC UA client can then read the content through the OPC UA file transfer methods.

A folder entry publishes the full tree: the files and the sub-folders. The gateway reads the folder again at a fixed interval and keeps the tree up to date. With access:, you tell which OPC UA roles can read, write, create and delete in the folder.

Words on this page​

WordMeaning
FileTypeThe OPC UA ObjectType for a file. A file entry needs a node of this type.
FileDirectoryTypeThe OPC UA ObjectType for a folder. A folder entry needs a node of this type.
folder scanThe gateway reads the folder on disk and adds or removes nodes to match it.
roleAn OPC UA role, for example Operator. Each user has one or more roles in the user store.
methodAn OPC UA method that a client calls: CreateFile, CreateDirectory, Delete on a folder; Open, Read, Write, Close on a file.

How to publish a file and a folder​

  1. Add an entry to instances: for the folder. Set typeDefinition: to ua:FileDirectoryType.
  2. Add an entry to instances: for the file. Set typeDefinition: to ua:FileType.
  3. Add an entry to mapping: for the folder. Set node: to the folder node and folder: to the path of the folder.
  4. Add an entry to mapping: for the file. Set node: to the file node and file: to the path of the file.
  5. Optional: set mimeType: on the file entry.
  6. Optional: set poll:, access: and ignore: on the folder entry.
  7. Start the gateway.

A mapping entry has node: and exactly one of file: and folder:.

A complete example​

instances:
- browseName: MyFile
typeDefinition: ua:FileType
nodeId: s=MyFile
organizedBy: /ua:Objects/ua:Server

- browseName: MyFolder
typeDefinition: ua:FileDirectoryType
nodeId: s=MyFolder
organizedBy: /ua:Objects/ua:Server

mapping:
- node: na=own;s=MyFolder
folder: ${TMP}/MyFolder

- node: na=own;s=MyFile
file: ${TMP}/MyFolder/MyFile.txt
mimeType: text/plain

This example comes from test/fixture/config5-files_and_folders.yml of the package.

Keys of a mapping entry​

KeyRequiredMeaning
nodeRequiredThe node to connect: a browse path, or a node id such as na=own;s=MyFolder.
folderOne of file and folderThe path of the folder in the local file system. The node must be of the type FileDirectoryType.
fileOne of file and folderThe path of the file in the local file system. The node must be of the type FileType.
mimeTypeOptional, with fileThe MIME type of the file. The schema lists the accepted values.
pollOptional, with fileThe interval to read the file again.
pollOptional, with folderThe interval of the folder scan, in milliseconds. At least 1000. The default is 5000.
accessOptional, with folderWhich roles can read, write, create and delete. See How to control access to a folder.
ignoreOptional, with folderThe files and sub-folders that the scan skips. See How to skip files in the scan.

The key reference is in mapping. The rules for all the mapping entries are in Mappings.

How the folder scan works​

The folder scan is automatic. You do not have to start it.

  • The gateway reads the folder when it starts, and then every poll: milliseconds. The default is 5000 ms.
  • For each new file, the gateway adds a FileType node. For each new sub-folder, it adds a FileDirectoryType node.
  • For each file or sub-folder that is gone, the gateway removes its node.
  • One scan reads the full tree, sub-folders included.
  • A program that writes into the folder (a PLC export, a script, a backup job) does not need OPC UA. The scan publishes its files.
mapping:
- node: /ua:Objects/ua:Server/own:MyFolder
folder: ${TMP}/MyFolder
poll: 2000

Set a short poll: when clients must see a new file quickly. Set a long poll: for a large tree: each scan reads all the folders.

note

The browse name of a node is the file name with each . replaced by _. The file report.csv becomes the node report_csv.

How to skip files in the scan​

Some programs write a file in two steps. First they write run42.part, then they rename it to run42.runlog. Without ignore:, a scan between the two steps publishes the incomplete file run42.part. A client can then read a file that is not complete.

Give the names to skip in ignore::

mapping:
- node: /ua:Objects/ua:Server/own:RunLogs
folder: /var/lib/omni-edge/runlogs
ignore: ["*.part", ".*"]
access:
read: [Observer, Operator, Engineer]

The scan does not publish run42.part. When the program renames the file, the next scan publishes run42.runlog.

These are the rules of a pattern:

  • The gateway compares the pattern with the path inside the folder, such as 2026/run42.part.
  • A pattern must match the full path.
  • * matches any characters, also /. ? matches one character, also /. The other characters, such as [, + and (, match only themselves.
  • The comparison is case-sensitive, also on Windows and macOS: *.part does not skip RUN.PART.
  • ! before a pattern keeps the files that the pattern matches. It keeps them even when an other pattern of the list also matches them: ["*.part", "!*"] skips nothing.
  • A skipped sub-folder gets no node, and the scan does not read its content. A ! pattern cannot keep a file inside a skipped sub-folder.
PatternSkips
*.partEvery .part file, in the folder and in all its sub-folders.
.*The files and sub-folders of the folder whose name starts with ..
*/.*The files and sub-folders whose name starts with ., in the sub-folders.
tmp/The sub-folder tmp of the folder, with all its content, and a file named tmp.
tmp/* and !tmp/keep.logAll the content of tmp, except tmp/keep.log. The folder tmp stays.
*.log and !keep.logEvery .log file, except keep.log.
note

To skip the dot files at every level, give both .* and */.*. The gateway changes these forms before it uses them:

  • A \ becomes /: runs\*.part is the same as runs/*.part.
  • A / or ./ at the start, and a / at the end, are removed: tmp/, /tmp and ./tmp are the same as tmp.
  • A // or /./ inside a pattern becomes /: 2026//*.part is the same as 2026/*.part.

A pattern cannot be empty, cannot start with #, and cannot start with ../.

ignore: changes only what the scan publishes. It does not change the files on the disk. It does not refuse the clients, but a file that a client makes with a skipped name, with CreateFile, does not stay: the next scan removes its node. The file stays on the disk. To limit the clients, use access:.

How to control access to a folder​

Without access:, every client can do everything: read, write, create files and folders, and delete.

With access:, you give a list of roles for each operation:

KeyThe roles in the list canIf you omit the key
browseSee the folders and the files.Every role.
readOpen a file and read its content.Every role that can browse.
writeOpen a file for writing and change its content. These roles can also read the file.Nobody.
createFileCall CreateFile.Nobody.
createDirectoryCall CreateDirectory.Nobody.
deleteCall Delete on a file or a sub-folder.Nobody.
noneNobody can call MoveOrCopy. The gateway does not implement it, with or without access:.Nobody.

Two rules:

  • Reading follows visibility. If you omit browse or read, all the roles that can see the folder can read.
  • Each change must be given. If you omit write, createFile, createDirectory or delete, no client can do it. An empty list [] means the same thing.

The folder scan of the gateway is never limited. With createFile: [], the files come only from the disk: no client can add a file.

A role in any list can also browse. You do not have to add it to browse.

The settings apply to the full tree: to the sub-folders and files that the scan finds, and to the sub-folders that a client creates.

Example: a folder that only the gateway fills​

Clients can read the reports. No client can add, change or delete a file.

mapping:
- node: /ua:Objects/ua:Server/own:Reports
folder: /var/lib/omni-edge/reports
poll: 5000
access:
read: [Observer, Operator, Engineer]

write, createFile, createDirectory and delete are omitted: nobody can do them.

Example: a folder for recipes​

Engineers upload recipe files. Operators read them. Only a supervisor can delete one. Nobody can create a sub-folder.

mapping:
- node: /ua:Objects/ua:Server/own:Recipes
folder: /var/lib/omni-edge/recipes
access:
browse: [Operator, Engineer, Supervisor]
read: [Operator]
write: [Engineer]
createFile: [Engineer]
createDirectory: []
delete: [Supervisor]

Which roles to use​

A role is one of these:

  • a standard role: Anonymous, AuthenticatedUser, Observer, Operator, Engineer, Supervisor, ConfigureAdmin, SecurityAdmin;
  • the browse name of a role of your model;
  • the node id of a role, for example ns=1;s=Maintenance.

These are the names that you give to users with users add --role. See How to add a user.

  • A client that connects without a user name has the role Anonymous.
  • Each user who logs in has the role AuthenticatedUser, plus the roles of the user store.
  • A Node-RED flow that uses Edge Link has the role of its token. To let a flow create files, put that role in createFile.

What the client sees​

A client that can see the folder sees all its methods, also the methods that it cannot call. A client that calls a method without the permission gets BadUserAccessDenied. The gateway does not change the disk.

The client callsNeeds the role in
CreateFile on a foldercreateFile
CreateDirectory on a foldercreateDirectory
Delete on a folderdelete
Open for reading, then Read, GetPosition, SetPosition, Closeread or write
Open for writing, then Writewrite
MoveOrCopyNot available. The gateway always refuses it.
tip

The roles control what OPC UA clients can do. They do not protect the disk. Give the folder to a dedicated account of the operating system, and let only the gateway write in it.

What the gateway does​

  • The gateway checks the type of the node. If the node is not of the correct type, the mapping entry does not start.
  • If the folder does not exist, the gateway makes it. The parent folder must exist.
  • If the file does not exist, the gateway makes an empty file. The parent folder must exist.
  • If the parent folder does not exist, the gateway shows a message in its log and skips the entry.
  • If the path exists, but is a folder where a file is expected (or a file where a folder is expected), the gateway skips the entry.
  • With poll: on a file entry, the gateway reads the file again at this interval. An interval of more than 10000 ms becomes 10000 ms.
  • With poll: on a folder entry, the gateway scans the folder at this interval. See How the folder scan works.

How to write the path​

A path can contain environment variables: ${TMP} or $TMP. The gateway replaces them when it starts.

If an environment variable is not set, the gateway reports the error Environment variable '...' is not defined. and the entry fails. The gateway does not replace the variable with an empty string.

A relative path is relative to the working directory of the gateway process. It is not relative to the folder of the configuration file. To avoid a mistake, use an absolute path.

Errors​

The gateway refuses to start when it finds one of these errors. It shows all the errors of all the mapping entries together.

CodeMeaningFix
FOLDER-E001A key of access: is not correct: an unknown key, a value that is not a list, or a role that the gateway cannot find.Use the keys of the table above. Write each role as a standard role name, the browse name of a role of your model, or a node id.
FOLDER-E002poll: of a folder entry is not a number of 1000 or more.Write a number of milliseconds, 1000 or more. on-demand is not accepted on a folder.
FOLDER-E003The gateway cannot apply access:: its file transfer component is too old. The gateway does not start, so that the folder does not stay open.Update the gateway.
FOLDER-E004ignore: is not a list of patterns, or a pattern is empty or starts with #.Write a list of texts, such as ["*.part", ".*"].
warning

Use a dedicated file system of a fixed size for the files and folders that you publish. An OPC UA client that writes files can then not fill the disk of the system.