Skip to main content

Write Variables to Azure Blob Storage

What This Example Does​

This example shows how to store the Web Service module payload (process variables) as a JSON file in Azure Blob Storage. Each request creates one JSON file with the current process variables. The file name is a timestamp so that no file is overwritten.

Why Use an Azure Function?​

An Azure Function gives you a safe execution environment next to Azure Blob Storage. The function authenticates against Blob Storage with Azure Managed Identity, so no storage key is needed in the code.

Prerequisites​

  • An Azure subscription with a Function App running Node.js 18 (or newer).
  • A storage account with a blob container.
  • The Function App must have a system-assigned managed identity with the Storage Blob Data Contributor role on the storage account.

Deploying the Azure Function​

  1. Create an HTTP-triggered function in your Function App (Azure Functions runtime 4).
  2. Add the application settings:
    • AZURE_STORAGE_ACCOUNT: the name of your storage account.
    • AZURE_CONTAINER_NAME: the name of the blob container.
    • AUTH_SECRET: a secret value that you also set as Authorization header in the Web Service module.
  3. Paste the code below into index.js.
const { BlobServiceClient } = require('@azure/storage-blob');
const { DefaultAzureCredential } = require('@azure/identity');

const storageAccount = process.env.AZURE_STORAGE_ACCOUNT || '';
const containerName = process.env.AZURE_CONTAINER_NAME || '';
const authSecret = process.env.AUTH_SECRET || '';

module.exports = async function (context, req) {
const { authorization } = req.headers || {};

// set the Authorization header like this in the Web Service module
if (authSecret && authorization !== `Bearer ${authSecret}`) {
context.res = { status: 403, body: 'Forbidden' };
return;
}

// req.body contains:
// req.body.params - params set in the web service module config
// req.body.variables - variables of the process instance / customer

const vars = req && req.body ? req.body.variables : null;
if (!vars) {
context.res = { status: 400, body: 'Missing variables' };
return;
}

const blobServiceClient = new BlobServiceClient(
`https://${storageAccount}.blob.core.windows.net`,
new DefaultAzureCredential()
);
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobName = `${Date.now()}.json`;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);

const content = JSON.stringify(vars);
await blockBlobClient.upload(content, content.length);

context.res = { status: 200, body: { ok: true, file: blobName } };
};

Using It in LoyJoy​

  1. Add a Web Service module to your process.
  2. Enter the URL of your deployed function.
  3. Set the Authorization header to Bearer <AUTH_SECRET> with the same value as in the function configuration.
  4. Publish and test: after the module runs, the JSON file with the current variables appears in your blob container.
note

This is example code. Check the container, permissions, and authentication in your own Azure environment before using it productively.