Problem with Azure Blob Storage and Azure Functions to store images.

Dirk Delbaere 1 Reputation point
2024-03-05T09:58:15.2366667+00:00

I tried this training to use Azure Blob Storage (and Azure Functions) to store images. Later I want to use it for Azure Cognitive services TTS to save and reuse mp3 speech files: https://learn.microsoft.com/en-us/training/modules/blob-storage-image-upload-static-web-apps/ . But it doesn't seem to work if I follow all the steps. Apparently this is an old training (works with Node.js V3. It asks to use Node.js 12.0 but I see that Node.js versions 10 and 12 aren't supported in Azure Functions 4.x. Are there any examples or training that are more recent using javascript, Azure Functions and Blob Storage?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,888 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,101 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,787 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,782 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Amira Bedhiafi 23,016 Reputation points
    2024-03-05T11:22:23.1666667+00:00

    The following table shows each version of the Node.js programming model along with its supported versions of the Azure Functions runtime and Node.js.

    User's image

    So if you are using Azure Functions 4.x , try to use Node.js 18.x +

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=javascript%2Cwindows%2Cazure-cli&pivots=nodejs-model-v4

    0 comments No comments

  2. MuthuKumaranMurugaachari-MSFT 22,306 Reputation points
    2024-03-06T17:05:56.8866667+00:00

    Dirk Delbaere Thanks for posting your question in Microsoft Q&A. You are right, training sample is using Node.js V3 and we suggest using newer model V4 for all new developments. We do have a migration guide to convert V3 to V4 Node.Js (which will be helpful) and for more V4 examples/code snippets, check out https://github.com/Azure/azure-functions-nodejs-samples/blob/main/js/src/functions.

    Code snippet:

    const { app } = require('@azure/functions');
    
    const {
        StorageSharedKeyCredential,
        ContainerSASPermissions,
        generateBlobSASQueryParameters,
    } = require("@azure/storage-blob");
    
    const { extractConnectionStringParts } = require('./credentials/utils');
    
    
    app.http('SASGeneration', {
        methods: ['GET', 'POST'],
        authLevel: 'anonymous',
        handler: async (request, context) => {
    
            const permissions = 'c';
            const container = 'images';
    
            const sasToken = generateSasToken(process.env.AzureWebJobsStorage, container, permissions);
            return { body: `"sasKey":"${sasToken.sasKey}","url":"${sasToken.url}"`};
        }
    });
    
    function generateSasToken(connectionString, container, permissions) {
        const { accountKey, accountName, url } = extractConnectionStringParts(connectionString);
        const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey.toString('base64'));
    
        var expiryDate = new Date();
        expiryDate.setHours(expiryDate.getHours() + 2);
    
        const sasKey = generateBlobSASQueryParameters({
            containerName: container,
            permissions: ContainerSASPermissions.parse(permissions),
            expiresOn: expiryDate,
        }, sharedKeyCredential);
    
        return {
            sasKey: sasKey.toString(),
            url: url
        };
    }
    
    
    

    I modified the code snippet from the training path for V4 as a reference and please adjust and validate for your actual use case scenarios.

    I hope this helps and let me know if any questions.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.