const { app } = require('@azure/functions'); const ExcelJS = require('exceljs'); const { Pool } = require('pg'); app.storageBlob('storageBlobTrigger', { path: 'attachments/{name}', connection: 'MyStorageConnectionString', handler: async (context) => { // Obtain a reference to the blob stream const blobStream = context.bindingData.blobTrigger.openReadStream(); try { context.log(`Processing blob "${context.bindingData.name}" with size ${context.bindingData.length} bytes`); // Create a new workbook const workbook = new ExcelJS.Workbook(); await workbook.xlsx.read(blobStream); // Read the Excel file from the blob stream // Assuming you're interested in the first worksheet const worksheet = workbook.getWorksheet(1); const pool = new Pool({ user: '****', host: '***', database: '***', password: '****', port: 5432, ssl: { rejectUnauthorized: false // Necessary for Azure connections but consider the security implications } }); // Connect to the database const client = await pool.connect(); try { // Start a transaction await client.query('BEGIN'); // Process all rows asynchronously const updates = []; worksheet.eachRow((row, rowNumber) => { const values = [ row.getCell(1).value, row.getCell(2).value, row.getCell(3).value, row.getCell(4).value, row.getCell(5).value, row.getCell(6).value, row.getCell(7).value, row.getCell(8).value ]; const updatePromise = client.query(` UPDATE stock_overview SET material_code = $2, producer_item_code = $3, description_id = $4, serial_number = $5, total_stock = $6, unit = $7, warehouse = $8 WHERE product_name = $1; `, values); updates.push(updatePromise); updatePromise.then(result => { context.log(`Updated database for Row ${rowNumber} with result: ${result.rowCount}`); }); }); // Wait for all updates to complete await Promise.all(updates); // Commit the transaction await client.query('COMMIT'); } catch (error) { await client.query('ROLLBACK'); // Roll back the transaction on error throw error; } finally { client.release(); // Release the client back to the pool } } catch (error) { context.log.error(`Failed to process Excel file: ${error}`); throw error; // Ensure errors are logged in Azure monitoring } finally { pool.end(); // Close the pool } } });
I am automating a process, in which:
-
I need to read email attachment files from Microsoft Outlook 365 account and update a database accordingly in the Microsoft Azure account.
-
I have created a logic app which triggers the email account and saves the attachment to a blob storage account. -beside that I have created a Azure Database for PostgreSQL flexible servers.
-
I have created a function which is triggered by the blob storage account and next step is to read and process the excel files and update the database. the code above I’ve written doesn’t work as intended and crashes. Could someone please help me with it.
Thank You for help in advance
2
Answers
Thanks Alot for your response. The function gets triggered by the storage account can read the excel files, it doesn’t update the Azure postgres database and my Function gets stuck here. I have to work with a vpn and the database is not found when connected with vpn.enter image description here
This worked for me.
I have used
xlsx
package for reading*.xlsx
file.INPUT
:OUTPUT
: