我需要在旧版 .NET Framework 应用程序 (4.8.1) 中实现 Azure 存储,我已使用 Azure.Storage.Blobs Nuget 包创建了一个 .NET Standard 2.0 类库,因为 Azure.Storage.Blob 已弃用。添加类库作为引用并尝试从 .NET Web 应用程序调用它时,会引发以下错误。
System.IO.FileNotFoundException
H结果=0x80070002
Message=无法加载文件或程序集“Azure.Storage.Blobs,Version=12.18.0.0,Culture=neutral,PublicKeyToken=92742159e12e44c8”或其依赖项之一。系统找不到指定的文件。
我尝试回滚到以前版本的 nuget 包,但错误是一样的。
我使用的代码位于azure SDK示例中(https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Azure.Storage.Blobs/samples/Sample01b_HelloWorldAsync)。 cs),并且仅尝试从 Web 表单应用程序调用类库时就会发生错误。
我在 .Net 4.8.1 中尝试了以下代码和包,并且 Web 应用程序成功运行,如下所示:-
Blob控制器:
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Web.Mvc;
public class BlobController : Controller
{
public async Task<ActionResult> UploadBlob()
{
string connectionString = "<connec_string>";
string containerName = "<container_name>";
string blobName = "<blob_name>";
string filePath = "<filepath>";
try
{
BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);
BlobClient blobClient = containerClient.GetBlobClient(blobName);
using (FileStream fs = System.IO.File.OpenRead(filePath))
{
await blobClient.UploadAsync(fs, true);
}
ViewBag.Message = "Blob uploaded successfully!";
}
catch (Exception ex)
{
ViewBag.Message = "Error: " + ex.Message;
}
return View();
}
}
UploadBlob.cshtml:
@{
ViewBag.Title = "Upload Blob";
}
<h2>Upload Blob</h2>
<p>@ViewBag.Message</p>
.csproj:
<Reference Include="Azure.Core, Version=1.35.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Core.1.35.0\lib\net472\Azure.Core.dll</HintPath>
</Reference>
<Reference Include="Azure.Storage.Blobs, Version=12.18.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Storage.Blobs.12.18.0\lib\netstandard2.0\Azure.Storage.Blobs.dll</HintPath>
</Reference>
<Reference Include="Azure.Storage.Common, Version=12.17.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8, processorArchitecture=MSIL">
<HintPath>..\packages\Azure.Storage.Common.12.17.0\lib\netstandard2.0\Azure.Storage.Common.dll</HintPath>
</Reference>
输出:
运行成功如下:
Azure 门户:
blob 已成功上传到存储,如下所示。