我一直在尝试设置一个Blobtrigger函数,当一个zip文件上传到一个容器时,它会解压缩并上传到同一个容器。我从Azure Function得到了解决方案:解压缩文件在调试中有效,但在生产中无效。
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 blobn 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");
}
}
}
}
我收到此错误:Microsoft.Azure.WebJobs.Host:无法将Blob绑定到类型"Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer">
原因如下:已尝试绑定到"Microsoft.Azure.Storage.Blob.CloudBlobDirectory,Microsoft.Azure.Storage.Blob,Version=111.1.7.0,Culture=neutral,PublicKeyToken=31bf3856a364e35",但用户类型程序集为"Microsoft.WindowsAzure.Sorage.Blob.CloudBlobContainer,Microsoft.WindowsAzure.Storage,Version=9.3.1.0
我不知道那个可能的原因到底意味着什么。我到处都看了,github和social.msdn,但没有答案。我不知道问题出在哪里。
您的程序集似乎太旧了,请安装最新的Microsoft.Azure.Storage.Blob
程序集:
https://www.nuget.org/packages/Microsoft.Azure.Storage.Blob/11.2.2
然后使用using Microsoft.Azure.Storage.Blob;
而不是using Microsoft.WindowsAzure.Storage.Blob;
。
csproj
文件如下:
<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>
关于它们之间的区别,你可以参考这篇文章:
Microsoft.Azure.Storage和WindowsAzure.Storage Nuget软件包之间有什么区别?
注意"重要的";变更日志的部分!
我也犯了同样的错误。
https://github.com/Azure/azure-webjobs-sdk/releases/tag/storage-v4.0.0
重要存储命名空间已更改,因此您可能需要更新如果您直接使用任何存储类型,请使用您的代码。你的代码可能会继续编译,因为您可能对旧的存储SDK,但如果没有已在您的代码中更新:
对于Blob和队列,Microsoft.WindowsAzure.Storage.*命名空间为现在是Microsoft.Azure.Storage.。对于表,命名空间现在是Microsoft.Azure。宇宙。表。.
检查您的使用情况;(