Zum Hauptinhalt springen

Variablen in Azure Blob Storage schreiben

Was dieses Beispiel tut​

Dieses Beispiel zeigt, wie das Payload des Web-Service-Moduls (Prozessvariablen) als JSON-Datei in Azure Blob Storage gespeichert wird. Jede Anfrage erstellt eine JSON-Datei mit den aktuellen Prozessvariablen. Der Dateiname ist ein Zeitstempel, sodass keine Datei überschrieben wird.

Warum eine Azure Function verwenden?​

Eine Azure Function bietet eine sichere Ausführungsumgebung direkt neben Azure Blob Storage. Die Funktion authentifiziert sich mit Azure Managed Identity gegen Blob Storage, sodass kein Storage-Schlüssel im Code benötigt wird.

Voraussetzungen​

  • Ein Azure-Abonnement mit einer Function App, die Node.js 18 (oder neuer) ausführt.
  • Ein Storage-Konto mit einem Blob-Container.
  • Die Function App muss eine systemzugewiesene Managed Identity mit der Rolle Storage Blob Data Contributor auf dem Storage-Konto haben.

Bereitstellen der Azure Function​

  1. Erstellen Sie eine per HTTP ausgelöste Funktion in Ihrer Function App (Azure Functions Runtime 4).
  2. Fügen Sie die Anwendungseinstellungen hinzu:
    • AZURE_STORAGE_ACCOUNT: der Name Ihres Storage-Kontos.
    • AZURE_CONTAINER_NAME: der Name des Blob-Containers.
    • AUTH_SECRET: ein geheimer Wert, den Sie auch als Authorization-Header im Web-Service-Modul setzen.
  3. Fügen Sie den folgenden Code in index.js ein.
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 } };
};

Verwendung in LoyJoy​

  1. Fügen Sie Ihrem Prozess ein Web-Service-Modul hinzu.
  2. Geben Sie die URL Ihrer bereitgestellten Funktion ein.
  3. Setzen Sie den Authorization-Header auf Bearer <AUTH_SECRET> mit demselben Wert wie in der Funktionskonfiguration.
  4. Veröffentlichen und testen: Nachdem das Modul ausgeführt wurde, erscheint die JSON-Datei mit den aktuellen Variablen in Ihrem Blob-Container.
hinweis

Dies ist Beispielcode. Prüfen Sie Container, Berechtigungen und Authentifizierung in Ihrer eigenen Azure-Umgebung, bevor Sie ihn produktiv verwenden.

Verwandte Seiten​