Skip to main content

Write Variables to Azure Blob Storage

danger

The following example code is not tested, yet. If you want to connect the process module WebService with an Azure Function, please contact LoyJoy customer success so that this code briefly can be finalized together with a LoyJoy developer in your Azure environment.

index.js

const { BlobServiceClient } = require('@azure/storage-blob')
require('dotenv').config()

module.exports = async function (context, req) {
const { DefaultAzureCredential } = require('@azure/identity')

const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME
if (!accountName) throw Error('Azure Storage accountName not found')

const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
new DefaultAzureCredential()
)

const { authorization } = req.headers

// set a authorization header like this in the process module
if (authorization !== 'Bearer TTYz6n6q7tu8MhBV7TCp') {
context.res = { status: 403, body: 'Unauthenticated' }
} else {
const vars = req?.body?.variables
const data = JSON.stringify(vars)

const containerClient = blobServiceClient.getContainerClient('insert blob container name')
const blockBlobClient = containerClient.getBlockBlobClient(`${Date.now()}.json`)
await blockBlobClient.upload(data, data.length)

context.res = {}
}
}

package.json

{
"name": "webservice_to_storage",
"version": "1.0.0",
"description": "WebService to Azure Function to Azure Blob Storage",
"private": true,
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@azure/identity": "^2.1.0",
"@azure/storage-blob": "^12.8.0",
"@types/dotenv": "^8.2.0"
}
}