Home > Mobile >  HTTP trigger azure function get image from blob storage error
HTTP trigger azure function get image from blob storage error

Time:01-30

I have an issue with my http trigger in azure functions. When I use the http trigger local the trigger is correctly getting the picture from the online azure storage container. Once azure function is deployed it no longer works.

Here is my code for the http trigger that work locally but not once deployed:

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Azure.Storage.Blobs;

namespace PlaygroundAzureFunctions
{
    public static class HttpFromStorage
    {
        [FunctionName("GetSnapsnot")]
        public static async Task<IActionResult> GetSnapsnot(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Snapshot")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation($"Snapsnot requested at: {DateTime.Now}");

            string Connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
            string containerName = Environment.GetEnvironmentVariable("ContainerName");
            var blobClient = new BlobContainerClient(Connection, containerName);
            var blob = blobClient.GetBlobClient("TestPicture.jpeg");

            var image = await blob.OpenReadAsync();

            log.LogInformation($"Snapsnot request returned at: {DateTime.Now}");

            return new OkObjectResult(image);
        }

    }
}

Here is my local.settings.json:

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "StringTakenFromCorrectStorageAccessKeysJustHiddenHere",
    "ContainerName": "file-upload", // container name
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

I was aiming for once deployed i could use the public to the internet app and use its url to call the api to trigger that specific picture to be shown.

The errors in the function monitor is as follows:

enter image description here

CodePudding user response:

local.settings.json is used only when you run locally and when the function is deployed the settings are missing.

You generally don't want to store your production secrets in your git repository and you should set them separately (as part of a deployment pipeline) but if this is for testing or if the resources are publicly available you can just put the settings in settings.json (no local).

You can also go to portal.azure.com->Your_App->Configuration=>Application Settings in Azure and set ContainerName directly there.

  • Related