Bitzsoft.Integrations.FileStorage.Nas 1.0.0-alpha.7

This is a prerelease version of Bitzsoft.Integrations.FileStorage.Nas.
dotnet add package Bitzsoft.Integrations.FileStorage.Nas --version 1.0.0-alpha.7
                    
NuGet\Install-Package Bitzsoft.Integrations.FileStorage.Nas -Version 1.0.0-alpha.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bitzsoft.Integrations.FileStorage.Nas" Version="1.0.0-alpha.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bitzsoft.Integrations.FileStorage.Nas" Version="1.0.0-alpha.7" />
                    
Directory.Packages.props
<PackageReference Include="Bitzsoft.Integrations.FileStorage.Nas" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bitzsoft.Integrations.FileStorage.Nas --version 1.0.0-alpha.7
                    
#r "nuget: Bitzsoft.Integrations.FileStorage.Nas, 1.0.0-alpha.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bitzsoft.Integrations.FileStorage.Nas@1.0.0-alpha.7
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bitzsoft.Integrations.FileStorage.Nas&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Bitzsoft.Integrations.FileStorage.Nas&version=1.0.0-alpha.7&prerelease
                    
Install as a Cake Tool

Bitzsoft.Integrations.FileStorage.Nas

NAS / 本地文件系统存储实现。

功能特性

  • 完整实现 IFileStore 接口:覆盖所有文件与存储桶操作
  • 扩展接口 INasFileStore:NAS 文件存储服务标识接口,支持未来扩展
  • 下载 URL 生成:通过 Domain + 路径拼接生成下载地址
  • 存储桶管理:创建、删除、判断存储桶是否存在(映射为 BasePath 下的子目录)
  • 文件元数据:保存文件时支持设置 ContentType
  • 配置提供器模式:通过 INasConfigProvider 抽象配置获取,支持静态配置或动态配置中心
  • IHttpClientFactory 集成:通过 Microsoft.Extensions.Http 管理 HTTP 连接
  • 无第三方 SDK 依赖:基于 System.IO 原生文件操作
  • 兼容所有 NAS 供应商:威联通、群晖、绿联、华为等,依赖操作系统 SMB/NFS 挂载

安装

.NET CLI

dotnet add package Bitzsoft.Integrations.FileStorage.Nas

PackageReference

<PackageReference Include="Bitzsoft.Integrations.FileStorage.Nas" Version="1.0.0" />

配置

appsettings.json

{
  "Nas": {
    "BasePath": "/mnt/nas",
    "DefaultBucketName": "default",
    "Domain": "http://cdn.example.com",
    "HttpClientName": "FileStorage",
    "DownloadUrlExpiration": 43200,
    "UploadUrlExpiration": 3600
  }
}
配置项 说明 默认值
BasePath 文件存储根目录,可指向本地路径或 NAS 挂载路径 --
DefaultBucketName 默认存储桶名称,映射为 BasePath 下的子目录 --
Domain 下载域名,用于生成下载 URL --
HttpClientName HttpClient 名称,用于 IHttpClientFactory 创建客户端 --
DownloadUrlExpiration 下载地址过期时间(秒) 43200
UploadUrlExpiration 上传地址过期时间(秒) 3600

注册服务

使用 DI 扩展方法(推荐)

using Bitzsoft.Integrations.FileStorage.Nas;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

// 注册 HttpClient 工厂
services.AddHttpClient("FileStorage");

// 一行注册 NAS 文件存储服务
services.AddBitzsoftNasFileStorage(options =>
{
    options.BasePath = "/mnt/nas";
    options.DefaultBucketName = "default";
    options.Domain = "http://cdn.example.com";
});

从 IConfiguration 绑定配置

using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Nas;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var services = new ServiceCollection();

// 注册 HttpClient 工厂(NasFileStore 通过 IHttpClientFactory 创建 HTTP 客户端)
services.AddHttpClient("FileStorage");

// 注册处理器工厂
services.AddSingleton<IFileNameProcessorFactory, DefaultFileNameProcessorFactory>();
services.AddSingleton<IBucketNameProcessorFactory, DefaultBucketNameProcessorFactory>();

// 从配置节绑定 NasOptions
services.AddSingleton<INasConfigProvider>(sp =>
{
    var options = configuration.GetSection("Nas").Get<NasOptions>();
    return new NasConfigProvider(options);
});

services.AddSingleton<INasFileStore, NasFileStore>();
services.AddSingleton<IFileStore>(sp => sp.GetRequiredService<INasFileStore>());

使用 IOptions 模式

using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Nas;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();

services.AddHttpClient("FileStorage");

services.AddSingleton<IFileNameProcessorFactory, DefaultFileNameProcessorFactory>();
services.AddSingleton<IBucketNameProcessorFactory, DefaultBucketNameProcessorFactory>();

// 配置选项
services.Configure<NasOptions>(
    configuration.GetSection("Nas"));

// NasConfigProvider 内部通过 IOptions<NasOptions> 获取配置
services.AddSingleton<INasConfigProvider, NasConfigProvider>();
services.AddSingleton<INasFileStore, NasFileStore>();
services.AddSingleton<IFileStore>(sp => sp.GetRequiredService<INasFileStore>());

使用示例

保存文件并获取下载链接

using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Nas;

public class ReportService
{
    private readonly INasFileStore _fileStore;

    public ReportService(INasFileStore fileStore)
    {
        _fileStore = fileStore;
    }

    public async Task<string> UploadReportAsync(Stream pdfStream, string reportName)
    {
        // 保存 PDF 到默认存储桶
        var result = await _fileStore.SaveFileAsync(
            pdfStream, $"{reportName}.pdf");

        // 生成 12 小时有效的下载链接
        var downloadUrl = await _fileStore.GenerateDownloadUrlAsync(
            result.FilePath);

        return downloadUrl;
    }
}

通过 IFileStore 抽象使用

using Bitzsoft.Integrations.FileStorage;

public class ContractService
{
    private readonly IFileStore _fileStore;

    public ContractService(IFileStore fileStore)
    {
        _fileStore = fileStore;
    }

    public async Task<FileResult> UploadContractAsync(
        Stream contractStream, string contractId)
    {
        var args = new SaveFileArgs($"{contractId}.pdf", contractStream)
        {
            ContentType = "application/pdf"
        };

        return await _fileStore.SaveFileAsync(args);
    }
}

从远程 URL 拉取文件到 NAS

using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Nas;

public class MediaSyncService
{
    private readonly IFileStore _fileStore;

    public MediaSyncService(IFileStore fileStore)
    {
        _fileStore = fileStore;
    }

    public async Task<FileResult> SyncExternalImageAsync(string imageUrl, string saveAs)
    {
        var args = new SaveFileByUrlArgs(saveAs, imageUrl)
        {
            BucketName = "media-assets"
        };

        return await _fileStore.SaveFileByUrlAsync(args);
    }
}

复制与移动文件

using Bitzsoft.Integrations.FileStorage;
using Bitzsoft.Integrations.FileStorage.Nas;

public class FileOrganizer
{
    private readonly IFileStore _fileStore;

    public FileOrganizer(IFileStore fileStore)
    {
        _fileStore = fileStore;
    }

    public async Task ArchiveFileAsync(string sourceFile, string archivePath)
    {
        // 复制文件到归档路径
        await _fileStore.CopyFileAsync(sourceFile, archivePath);

        // 删除原文件(等同于 MoveFileAsync)
        await _fileStore.DeleteFileAsync(sourceFile);
    }

    public async Task MoveToBucketAsync(
        string fileName, string sourceBucket, string destBucket)
    {
        var sourceArgs = new FileStorageArgs(fileName) { BucketName = sourceBucket };
        var destArgs = new FileStorageArgs(fileName) { BucketName = destBucket };

        await _fileStore.MoveFileAsync(sourceArgs, destArgs);
    }
}

核心类型一览

类型 说明
INasFileStore NAS 文件存储接口,继承 IFileStore
NasFileStore 默认实现类
NasOptions NAS 存储配置
INasConfigProvider 配置提供器接口
NasConfigProvider 默认配置提供器(支持 IOptions<NasOptions>
ServiceCollectionExtensions DI 扩展方法 AddBitzsoftNasFileStorage

部署场景

本实现基于 System.IO 原生文件操作,不依赖任何第三方 SDK 或网络协议。文件的存储位置完全由 BasePath 配置决定,支持以下两种典型场景。

场景一:本地文件系统(小项目 / 开发环境)

将文件存储在项目发布目录内或服务器本地路径,适用于单机部署、开发测试或小型应用。

目录结构:

/var/www/myapp/              ← 应用发布目录
├── app.dll
├── appsettings.json
└── data/                    ← BasePath 指向此目录
    ├── invoices/            ← 存储桶(子目录)
    │   ├── 2024-001.pdf
    │   └── 2024-002.pdf
    └── avatars/             ← 存储桶(子目录)
        └── user-123.jpg

配置:

{
  "Nas": {
    "BasePath": "/var/www/myapp/data",
    "DefaultBucketName": "uploads",
    "Domain": "https://myapp.example.com/data"
  }
}

生成的下载 URL 示例:https://myapp.example.com/data/invoices/2024-001.pdf

需要在 Web 服务器(如 Nginx)中配置 /data 路径的静态文件服务,将请求映射到 BasePath 目录。

场景二:NAS 挂载(生产环境 / 多实例)

将文件存储在 NAS 设备的挂载路径,适用于多实例部署、容器环境或需要集中管理文件的场景。支持所有主流 NAS 供应商。

前提条件:

运维人员需要先将 NAS 共享目录挂载到服务器本地路径:

# 威联通 QNAP(SMB 挂载)
mount -t cifs //192.168.1.100/share /mnt/nas -o username=admin,password=xxx

# 群晖 Synology(NFS 挂载)
mount -t nfs 192.168.1.101:/volume1/data /mnt/nas

# 绿联 UGREEN(SMB 挂载)
mount -t cifs //192.168.1.102/public /mnt/nas -o username=admin,password=xxx

# 华为家庭存储(NFS 挂载)
mount -t nfs 192.168.1.103:/share /mnt/nas

# Windows 共享目录(SMB/CIFS)
net use Z: \\192.168.1.100\share /user:admin xxx

目录结构:

/mnt/nas/                    ← BasePath 指向 NAS 挂载点
├── invoices/                ← 存储桶
├── contracts/               ← 存储桶
└── media/                   ← 存储桶

配置:

{
  "Nas": {
    "BasePath": "/mnt/nas",
    "DefaultBucketName": "default",
    "Domain": "https://files.mycompany.com"
  }
}

应用层面无需关心底层 NAS 协议(SMB/NFS/CIFS),只需操作挂载后的本地路径。NAS 供应商的兼容性由操作系统保证。

场景对比

对比项 本地文件系统 NAS 挂载
适用规模 小型项目、开发测试 生产环境、多实例部署
文件持久性 随发布目录,升级可能丢失 独立存储,不受应用升级影响
多实例共享 不支持 支持(通过 SMB/NFS)
供应商依赖 无(依赖操作系统挂载)
运维要求 需配置挂载并保证可用性

依赖

  • Bitzsoft.Integrations.Compatibility,基础工具库
  • Bitzsoft.Integrations.FileStorage,多云文件存储抽象层
  • Microsoft.Extensions.Http,HttpClient 工厂支持

相关包

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Bitzsoft.Integrations.FileStorage.Nas:

Package Downloads
Bitzsoft.Integrations.All

Bitzsoft 第三方集成聚合包 — 包含全部 Integration 模块

Bitzsoft.Integrations.FileStorage.All

多云文件存储聚合包 — 包含 Aliyun / Azure / AWS / MinIO / 腾讯云 / 华为云 / 七牛云 / 又拍云 / 金山云 / NAS 全部实现

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-alpha.7 60 6/16/2026
1.0.0-alpha.6 58 6/16/2026
1.0.0-alpha.5 59 6/14/2026
1.0.0-alpha.3 62 6/7/2026