Working with Files
During the execution of an Integration use-case, there might be a need to work with files in one way or another. There are two different types of files to use, local files (or temporary files) and Job files. The latter is used for persisting any file along with the Job, for example the outbound payload (the result of the job execution) is stored in such job file.
The former, the local files, are files used temporarily during the execution for some purpose. It could be for storage of data downloaded from another service, or some temporary computed data. These files are cleaned up automatically when the Job is completed.
Local Files
Local files are created using the Local File API.
An example of using this is shown below:
const api = context.localFileAPI();
const file1 = api.create('file1.txt');
const file2 = api.create('subdir/file2.txt');
const file3 = api.create('subdir/file3.txt');
| Local files are deleted as soon as the code execution completes. |
Note that you can also use the HTTP API for downloading data from a remote system into a local file.
const file = context.localFileAPI().create('remote_file.xml');
const httpAPI = context.httpAPI();
const response = httpAPI.newRequestBuilder()
.url('https://service')
.method('GET')
.build()
.download(file);
if (response.succeeded()) {
}
To store the input data given to the Job into a local file, do like below:
// Use case: A job triggered by a REST call with file upload
// and you need to access the uploaded files in the Job.
const fileAPI = context.localFileAPI();
const inputData = context.input().data();
// Create the local file
const zipFile = fileAPI.create('myfile.zip');
// Save input data into the local file
const bytesSaved = zipFile.save(inputData.stream());
console.debug('Input data saved (%i bytes)');
// Extract the ZIP file now into the 'content' directory
fileAPI.extract(zipFile, 'content');
Job Files
Job files are created using the Data Storage API.
Each file is associated with the following meta-data:
-
ID (Assigned by TIF Cloud on store)
-
name (required)
-
description (optional)
-
data-type (required). One of OUTBOUND, INBOUND or OTHER
-
content-type (optional but recommended to be set)
-
character-set (optional, but recommended to be set. Defaults to UTF-8)
Files can either contain character based data OR binary data.
To create a string based file, see below for an example
const dataStorage = context.dataStorage();
const content = 'this is the content of the file...';
const file = dataStorage.newStringDataBuilder()
.data(content)
.outboundData()
.name('data.txt')
.description('Description of content')
.contentType('text/plain')
.build();
const fileId = dataStorage.store(file);
To create binary data:
const storage = context.dataStorage();
// binary data can be created differently
const binaryData = new Uint8Array(100);
//...
const fileId = dataStorage.store(storage
.newByteArrayDataBuilder()
.data(binaryData)
.outboundData()
.name('temp.bin')
.build());
Or create a job-file from a Local File:
const file = context.localFileAPI().create('test.txt');
const data = file.toData(null, 'text/plain', 'ISO-8859-15', null);
context.dataStorage().store(data);
Note, the toData method sets the data-type of the data instance to OTHER.
To make it of type OUTBOUND, then use the method toOutboundData instead.