使用 Wisej.NET 访问 Microsoft Azure Blob 存储





5.00/5 (12投票s)
本文档介绍了如何在 .NET Web 应用程序中使用 Azure 文件的好处。
引言
Wisej.NET 框架在文件处理方面非常灵活和可扩展。它为文件访问提供了一个抽象接口,使客户能够使用其 Web 应用程序所需的任何文件系统:无论是用于标准基于磁盘的文件系统的内置默认实现,还是用于基于云的文件存储的几种替代实现。
如今,将 Web 应用程序部署到 Microsoft Azure 等环境变得很有吸引力。 因此,使用 Azure 文件集成作为传统本地文件服务器的补充或完全替代方案是有意义的。
如何将 Azure Blob 存储与 Wisej.NET 一起使用
在本文中,我将介绍 Wisej.NET Web 应用程序如何与存储在 Microsoft Azure Blob 存储上的云文件进行交互。 要将 Web 应用程序连接到 Azure Blob 存储,需要执行几个简单的步骤。
必备组件
- Azure 订阅
- Azure 存储帐户
- 引用 Wisej.NET
- 在您的解决方案中安装
Azure.Storage.Blobs
12.10 库 - 安装
System.Runtime.CompilerServices.Unsafe
版本 4.6.0编译器服务将需要在 Web.config 文件中进行绑定重定向
- 安装
System.Buffers
版本 4.5.1
System.Buffers
将需要在 Web.config 文件中进行绑定重定向
AzureBlobProvider 的实现
Azure Blob 存储的文件系统实现需要放置在一个实现 Wisej.NET IFileSystemProvider
的类中,我将我的新类命名为 AzureBlobProvider
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wisej.Core;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Threading.Tasks;
using System.Text;
using System.IO;
using System.Configuration;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using Azure;
namespace AzureBlobStorageSample
{
/// <summary>
/// Implementation of the Azure Blob Storage provide.
/// Provides access to the Azure Blog Storage as a file system.
/// </summary>
public class AzureBlobProvider : IFileSystemProvider
{
/// <summary>
可以使用多种方法进行 Blob 存储的身份验证,例如 Microsoft 文档 指示的那样 – 实际的身份验证不在本文的当前范围内,而是文件访问部分更为相关。 独立于身份验证类型,对于实际的 Azure Blob 存储连接,我们需要知道帐户和容器名称 – 这存储在我们的 AzureBlobProvider
类成员中
/// </summary>
public string AccountName
{
get { return this.accountName; }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("accountName");
this.accountName = value.Trim();
}
}
private string accountName;
/// <summary>
/// Returns or sets the name of the Azure Blob Container
/// </summary>
internal string BlobContainer
{
get { return this.blobContainer; }
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentNullException("blobContainer");
this.blobContainer = value.Trim();
}
}
private strong blobContainer;
方案 1:导入操作
对于标准导入操作,最终用户应该能够从 Azure 存储中选择文件路径。 为了实现文件导入,我使用了一个 Wisej.NET Open File Dialog,我在其中添加了一个本地文件夹和一个 Azure Blob 存储容器,只是为了展示两个文件系统可以一起使用
private void btOpenFile_Click(object sender, EventArgs e)
{
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Title = "Select file to import";
openFileDialog.Roots.Add(new FileSstemProvider("\\network shared path\subpath01\... ", "My Local Files"));
openFileDialog.Roots.Add(this.AzureBlobProvider);
openFileDialog.Filter = "Text Files (*.txt)|*.txt|CSV Files (*.csv)|*.csv|XML Files (*.xml)|*.xml";
openFileDialog.FilterIndex = 3;
openFileDialog.AddExtension = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtImportFilePath.Text = openFileDialog.FileName;
}
}
}
如果我们运行示例,系统会提示用户一个文件选择器对话框 – 如图所示,显示了 Azure Blob 存储文件夹结构
这是可能的,因为 Wisej.NET Open File Dialog 从我们的 AzureBlobProvider
类调用 GetDirectories
和 GetFiles
方法
为了显示文件属性,AzureBlobProvider
类实现了诸如 GetFileSize
或 GetCreationTime
之类的方法。
在用户选择导入的文件后,Web 应用程序可能需要处理文件内容。 在服务器端,此操作意味着下载 Azure Blob 项目并读取其内容。
下载和读取 Blob 项目内容的实现如下
方案 2:导出操作
另一个常见的情况是,Web 应用程序生成一个报告,该报告应上传到预定义或用户定义的 Azure 位置。 为了模拟导出,在我的示例中,您可以键入文件内容,然后单击“上传”按钮
单击“上传”按钮后,“保存文件”对话框允许您指定文件的位置和名称
结果将上传到 Azure 存储
结论
Wisej.NET 是一个非常强大的 Web 应用程序框架,它使您可以利用最新的 .NET 技术。
有关如何实现云存储文件访问的更多示例,我推荐 ITG 提供的 Amazon S3 文件系统实现,这也是我开发 Azure 存储文件系统提供程序的支持。
wisej-extensions · iceteagroup/wisej-extensions (github.com)
历史
- 2022 年 8 月 16 日:初始版本