0

I am trying to create a Azure Function that uses a BlobTrigger to manipulate some files and then save them back into the blob.

To do this I want to be able to access the BlobProperties object to check the Content-Type of the file, to ensure it is supported by my manipulation routine.

It is my understanding from this article that I should simply be able to add a parameter on method called Properties which is of type BlobProperties and I can confirm this works for the other Metadata types listed.

However whenever I add the properties, my app does not work and reports the following error:

The 'Function1' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Function1'. Microsoft.Azure.WebJobs.Host: Can't bind parameter 'Properties' to type 'Microsoft.WindowsAzure.Storage.Blob.BlobProperties'.

What am I doing wrong? Below is my method:

public static void Run([BlobTrigger("TestContainer/{name}", Connection = "AzureWebJobsStorage")] Stream inputFile,
        string name, string BlobTrigger, IDictionary<string, string> Metadata, ILogger log, BlobProperties Properties)

My target framework is .NET Core 3.1 and the Azure Functions Version is v3. I have the following NuGet packages:

  • Microsoft.Azure.WebJobs.Extensions.Storage 4.0.3
  • Microsoft.NET.Sdk.Functions 3.0.11

I saw a similar post which suggested removing the reference to Extensions.Storage component, but doing so removes the [BlobTrigger] attribute and other types, so does not work. Related questions seem to date back to 2018 and are for older versions of Azure Function, surely this should just work out the box?

Appreciate any suggestions, thank you in advance.

Ryan Thomas
  • 1,724
  • 2
  • 14
  • 27
  • The parameters types of the azure function are limited. Why don't you treat blobs as objects like blockblob but as streams? I think classes like blockblob should not cause serialization problems(I seem to have done something similar before.). It should be feasible to pass in the blob object and operate it, you can have a try. – Cindy Pau Dec 10 '20 at 10:55
  • @BowmanZhu sorry, I'm not sure I understand what you mean, are you able to provide an example? Are you suggesting changing the inputFile to type CloudBlockBlob? – Ryan Thomas Dec 10 '20 at 11:01
  • I have posted the answer, you can have a look and try. – Cindy Pau Dec 11 '20 at 02:01

1 Answers1

2

Just do like below:

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;

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

I can successfully get the content-type of blob:

enter image description here

And the reference package:

<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.10" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • Yeah this works fine in version 3.0.10 but fails in 4.0.3. Thank you for your help, I think for now I will stick with using a Stream, and use the IDictionary Metadata binding to check for "hdi_isfolder" and I can also use {name}.{extension} to filter out folders too :) – Ryan Thomas Dec 11 '20 at 14:15
  • Version 3.0.10 also has a problem with a "LogType not present in the dictionary" but this is resolved in 3.0.11 and have got it working as expected. Thank you. – Ryan Thomas Dec 23 '20 at 10:54
  • WindowsAzure.Storage is legacy, Azure.Storage.Blobs seem to be wrong too. Try removing them and also add Microsoft.Azure.Storage.Blob NuGet package. – EladTal Nov 28 '22 at 23:48