azure 函数应用程序连接到 postgresql

问题描述 投票:0回答:1
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
        }
    }
});

我正在自动化一个流程,其中:

  • 我需要从 Microsoft Outlook 365 帐户读取电子邮件附件文件,并在 Microsoft Azure 帐户中相应更新数据库。

  • 我创建了一个逻辑应用程序,它会触发电子邮件帐户并将附件保存到 Blob 存储帐户。 - 除此之外,我还为 PostgreSQL 灵活服务器创建了 Azure 数据库。

  • 我创建了一个由 blob 存储帐户触发的函数,下一步是读取和处理 Excel 文件并更新数据库。我编写的上面的代码无法按预期工作并崩溃。有人可以帮我吗?

谢谢您提前的帮助

node.js excel azure office365
1个回答
0
投票

这对我有用。

我使用了

xlsx
包来读取
*.xlsx
文件。

const { app } = require('@azure/functions');
const {Client} = require('pg');
const XLSX = require('xlsx');

app.storageBlob('storageBlobTrigger', {
    path: 'test',
    connection: 'mystrg_conn',
    handler: async (blob,context) => {
        const connection = new Client(
            {
                host: "xxxxxxxx",
                user: "xxxxxxxxx",
                password: "xxxxxxxxx",
                database: "xxxxxxx",
                port: 5432,
                ssl:{
                    rejectUnauthorized: false
                }
            });
            try{
            await connection.connect();

            context.log(`Storage blob function processed blob "${context.triggerMetadata.name}" with size ${blob.length} bytes`);
            console.log("excel data: ",blob)

            const workbook = XLSX.read(blob);
            const sheetName = workbook.SheetNames[0] // Assuming data is in the first sheet
            const sheet = workbook.Sheets[sheetName];

            const data = XLSX.utils.sheet_to_json(sheet);

            console.log(data);
            for (let row of data) {
                await connection.query('INSERT INTO desserts (name, type, price) VALUES ($1, $2, $3)', [row.name, row.type, row.price]);
            }
            

            const result = await connection.query('Select * from desserts')

            console.log("Result: ",result.rows);

        }
        catch(error){
            console.log("Error: ",error);
        }
        finally{
            connection.end()
        }

            
    }
});

INPUT

名字 类型 价格
奶油糖果 冰淇淋 9.50

OUTPUT

[
  {
     dessert_id: 1,
     name: 'Chocolate Cake',
     type: 'Cake',
     price: '15.99'
   },
   {
     dessert_id: 2,
     name: 'Vanilla Ice Cream',
     type: 'Ice Cream',
   },
   {
     dessert_id: 3,
     name: 'Strawberry Cheesecake',
     type: 'Cheesecake',
     price: '20.75'
   },
   { dessert_id: 4, name: 'Apple Pie', type: 'Pie', price: '12.25' },
   {
     dessert_id: 5,
     name: 'Butterscoch',
     type: 'Ice Cream',
     price: '9.50'
   }
]

© www.soinside.com 2019 - 2024. All rights reserved.