Read data from Azure Blob Storage Using Mirth
Note: Mirth version used here is 4.5.0
Libraries that Mirth 4.5.0 would need to read the data from Microsoft Azure Blob Storage is as below:
azure-core-1.48.0.jar
azure-core-http-netty-1.14.2.jar
azure-identity-1.12.0.jar
azure-json-1.1.0.jar
azure-storage-blob-12.25.3.jar
azure-storage-common-12.24.3.jar
azure-xml-1.0.0.jar
jackson-annotations-2.13.5.jar
jackson-core.2.13.5.jar
jackson-databind-2.13.5.jar
jackson-dataformat-xml-2.13.5.jar
jackson-datatype-jsr310–2.13.5.jar
netty-handler-4.1.101.Final.jar
netty-handler-proxy-4.1.101.Final.jar
netty-resolver-4.1.101.Final.jar
netty-resolver-dns-4.1.101.Final.jar
If you miss any of the above mentioned libraries then it won’t work. Make sure the libraries are properly places in the mirth custom lib or added in resource location before you go to the below section
// Put your default connection string protocol here
var string = "DefaultEndpointsProtocol=…";
var containerName = $cfg('SOURCE_CONTAINER');
var containerClient = new com.azure.storage.blob.BlobServiceClientBuilder().connectionString(string).buildClient().getBlobContainerClient(containerName);
var blobItems = containerClient.listBlobs();
var iterator = blobItems.iterator();
var bufferValue = 1024;
while(iterator.hasNext()){
var blobItem = iterator.next();
var blobClient= containerClient.getBlobClient(blobItem.getName());
var outputStream = new java.io.ByteArrayOutputStream();
channelMap.put('fileName',blobItem.getName());
try{
var inStream = blobClient.openInputStream();
var bytesRead;
var buffer = java.lang.reflect.Array.newInstance(java.lang.Byte .TYPE, bufferValue);
while((bytesRead = inStream.read(buffer))!=-1){
outputStream.write(buffer, 0, bytesRead);
}
var fileContent = new java.lang.String(outputStream.toByteArray(), java.nio.charset.StandardCharsets.UTF_8);
// print the filecontent and see if it extracts it exactly as uploaded in the storage explorer
}catch(Exception){
logger.debug("exception "+Exception);
}
}
To read the file content from the azure blob storage use the below code in mirth
Writing a incoming message into Azure Blob Storage Container
The below code is to demonstrate how to write a incoming message in mirth to a azure blob storage container.
var connectionString = "DefaultEndpointsProtocol….";
var uploadContainerName = $cfg('FOLDER_1');
var deleteContainerName = $cfg('FOLDER_2');
var failedContainerName = $cfg('FOLDER_3');
var fileName = $s('fileName');
var bufferValue = 1024;
var result;
try{
var containerClient = new com.azure.storage.blob.BlobServiceClientBuilder().connectionString(connectionString).buildClient().getBlobContainerClient(uploadContainerName);
var uploadBlobClient= containerClient.getBlobClient(fileName);
var inputStream= new java.io.ByteArrayInputStream(connectorMessage.getRawData().getBytes());
uploadBlobClient.upload(inputStream);
}catch(Exception){
result = 'There is an error in uploading the file to a container '+Exception.toString();
logger.debug("Exception - "+Exception)
}
return result;