How to use Azurite blob containers as Azure Function internal storage and as trigger?

8.2k views Asked by At

I've followed the instructions to download and install Azurite for Linux (Ubuntu 18.04). I want to use it with Azure Storage Explorer (also downloaded and installed) to visually manage/test an Azure Function with BlobTrigger. I understand how to start Azurite and can upload blobs to an emulator container using Storage Exporer.

Cannot figure out:

  1. How to connect an Azure Function to an Azurite container to use as the Functions internal storage.

    a. I used "AzureWebJobsStorage": "UseDevelopmentStorage=true" in local.settings.json, but I don't see how that connects the Function to a given container in Azurite

  2. How to connect the Function to an Azurite container for BlobTrigger functionality.

    a. Do I need to add a "BlobTrigger": "<azuriteContainerConnectionString>" setting to local.settings.json?

2

There are 2 answers

6
Cindy Pau On BEST ANSWER

Basically, The Values in local.settings.json is been used to save the environment variable.

The connection string is been declared in function.json. If you are using some language like C# or Java(Languages that need to be compiled, not run directly.), then it always have a declaration part, the declaration part will be convent to function.json after compiled.

I start a Azurite on local, and I try to use the default storage account:

I get the default connection string of blob service:

DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;

And I create a C# azure function with blobtrigger:

local.settings.json

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "str": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;"
  }
}

Function1.cs

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace FunctionApp1
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("test/{name}", Connection = "str")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
    }
}

It seems works fine:

enter image description here

I set AzureWebJobsStorage to a storage on azure because 10000 port is been used.

This is the doc:

https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azurite?toc=/azure/storage/blobs/toc.json
0
DisplayName On

Using the Microsoft Azure Storage Explorer to manage queueing, the following settings will work.

(In this case the framework used was .net 5).

Its not particularly obvious but you must set the connection string to use the value "UseDevelopmentStorage=true"

My local.settings.json file after creating a new function enter image description here My function enter image description here