Home > Net >  Create an Azure AppendBlobClient logfile
Create an Azure AppendBlobClient logfile

Time:06-07

I am attempting to write log file entries to an Azure Blob file using the AppendBlobClient:

const { BlobServiceClient } = require('@azure/storage-blob');
const moment = require('moment-timezone');
const AZURE_STORAGE_CONNECTION_STRING = process.env["FOO_CONNECTION_STRING"];
const FOO_CONTAINER = process.env["FOO_CONTAINER_NAME"];

exports.handler = async (context, req) => {
    // setup logging to Azure Blob Container
    const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

    const logFileName = "log"   moment().tz('America/Chicago').format("YYYYMMDD");
    const containerClient = blobServiceClient.getContainerClient(FOO_CONTAINER);
    containerClient.create(logFileName);
    const loggingClient = containerClient.getAppendBlobClient(logFileName);

    const orders = req.body

    // iterate over collection of orders, executing an insert for each
    for (let idx = 0; idx < orders.length; idx  ) {
       // do an insert, capture the results, create a log entry
       await loggingClient.appendBlock(logEntry, logEntry.length);
    }
        
    return {
        statusCode: 200,
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify( { insertionCount: orders.length })
    };
}

However, I am struggling to understand how to create the initial, empty blob logfile (of type AppendBlob). If I execute this, I get an error that the The specified blob does not exist. so obviously, containerClient.create(logFileName); is not correct. Anyone use AppendBlobClient that could point out what I am doing wrong?

CodePudding user response:

Figured out the issue, was attempting the create on the wrong object. It should be:

const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);

const logFileName = "log"   moment().tz('America/Chicago').format("YYYYMMDD");
const containerClient = blobServiceClient.getContainerClient(FOO_CONTAINER);
const loggingClient = containerClient.getAppendBlobClient(logFileName);
await loggingClient.create();
  • Related