简体   繁体   中英

Azure function binding to container error when unzipping file to blob storage

I have been trying to set up a Blobtrigger function that when a zip file is uploaded to a container it unzips and uploads it to the same container. I got the solution from Azure Function: Unzip file works in debug but not in production .

public static class Function1
   {
       [FunctionName("Function1")]
       public static async Task Run([BlobTrigger("raw-nppes/{inputBlobName}", Connection = "key")] Stream inputBlob, string inputBlobName,
           Binder binder,
           ILogger log)
       {
           log.LogInformation($"Blob trigger function received blob\n Name:{inputBlobName} \n Size: {inputBlob.Length} Bytes");

           if (Path.GetExtension(inputBlobName)?.ToLower() == ".zip")
           {
               // We use the first char of the input file name as a dynamic part in the container. (Note: You should check if this is a valid char for the container name)
               var container = $"my-dynamic-container-{inputBlobName.Substring(0, 1).ToLower()}";
               var attributes = new Attribute[]
               {
                       new BlobAttribute($"{container}", FileAccess.ReadWrite),
                       new StorageAccountAttribute("AzureWebJobsStorage")
               };
               var outputContainer = await binder.BindAsync<CloudBlobContainer>(attributes);
               await outputContainer.CreateIfNotExistsAsync();

               var archive = new ZipArchive(inputBlob);
               foreach (var entry in archive.Entries)
               {
                   // we write the output files to a directory with the same name as the input blob. Change as required
                   var blockBlob = outputContainer.GetBlockBlobReference($"{inputBlobName}/{entry.FullName}");
                   using (var fileStream = entry.Open())
                   {
                       if (entry.Length > 0)
                       {
                           log.LogInformation($"Extracting - {entry.FullName} to - {blockBlob.Name}");
                           await blockBlob.UploadFromStreamAsync(fileStream);
                       }
                   }
               }
           }
           else
           {
               log.LogInformation("Not a zip file. Ignoring");
           }
       }
   }
}

I got this error: Microsoft.Azure.WebJobs.Host: Can't bind Blob to type 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer'

with this possible cause: Tried binding to 'Microsoft.Azure.Storage.Blob.CloudBlobDirectory, Microsoft.Azure.Storage.Blob, Version=11.1.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' but user type assembly was 'Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer, Microsoft.WindowsAzure.Storage, Version=9.3.1.0

I don't know what that possible cause actually means. I have looked EVERYWHERE on here, github, and social.msdn but no answer. I have no idea what the issue is.

It seems that your assembly is too old, please install the latest Microsoft.Azure.Storage.Blob assembly:

https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/11.2.2

Then use using Microsoft.Azure.Storage.Blob; instead of using Microsoft.WindowsAzure.Storage.Blob; .

The csproj file like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.2.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="4.0.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Regarding the difference between them, you can refer to this post:

What is the difference between the Microsoft.Azure.Storage and WindowsAzure.Storage Nuget packages?

Pay attention on the "Important" section of the Changelog!

I was doing the same error.

https://github.com/Azure/azure-webjobs-sdk/releases/tag/storage-v4.0.0

Important Storage namespaces have changed, so you may need to update your code if you are using any of the Storage types directly. Your code may continue to compile as you may have an implicit reference to the old Storage SDK, but it will fail at runtime if these have not been updated in your code:

For blobs and queues, Microsoft.WindowsAzure.Storage.* namespaces are now Microsoft.Azure.Storage. . For tables, the namespaces are now Microsoft.Azure.Cosmos.Table. .

Check your using;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM