You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

2023年如何向Azure File Share上传大于4MB的文件?

向Azure File Share上传大于4MB文件的正确方法(.NET)

首先明确:Azure.Storage.Files.Shares v12+ 并没有4MB的上传限制——默认单块上传大小是4MB,但SDK会自动对超过该阈值的文件启用分块上传,完全支持上传最大100TB的单个文件。以下是两种常用的实现方式:

1. 自动分块上传(推荐,无需手动处理)

使用ShareFileClient.UploadAsync方法时,SDK会自动检测文件大小,超过4MB就自动分块上传。你可以通过配置选项自定义分块大小、并发数等参数:

using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;

// 初始化客户端
var connectionString = "你的存储账户连接字符串";
var shareName = "目标文件共享名称";
var localFilePath = @"C:\path\to\your\large-file.bin";
var remoteFileName = "uploaded-large-file.bin";

var shareClient = new ShareClient(connectionString, shareName);
await shareClient.CreateIfNotExistsAsync();

var fileClient = shareClient.GetFileClient(remoteFileName);

// 可选:自定义上传配置(比如设置100MB单块、4并发)
var uploadOptions = new ShareFileUploadOptions
{
    TransferOptions = new StorageTransferOptions
    {
        MaximumTransferSize = 100 * 1024 * 1024, // 100MB/块
        MaximumConcurrency = 4 // 并行上传的块数
    }
};

// 自动处理分块,直接上传
await fileClient.UploadAsync(localFilePath, uploadOptions);

2. 手动分块上传(适合流式/断点续传场景)

如果需要自定义控制每一块的上传逻辑(比如从网络流读取、实现断点续传),可以手动调用分块上传API:

using Azure.Storage.Files.Shares;
using Azure.Storage.Files.Shares.Models;

var connectionString = "你的存储账户连接字符串";
var shareName = "目标文件共享名称";
var remoteFileName = "uploaded-large-file.bin";
var localFilePath = @"C:\path\to\your\large-file.bin";
var blockSize = 100 * 1024 * 1024; // 100MB/块

var shareClient = new ShareClient(connectionString, shareName);
var fileClient = shareClient.GetFileClient(remoteFileName);

// 先创建对应大小的空文件
var fileInfo = new FileInfo(localFilePath);
await fileClient.CreateAsync(fileInfo.Length);

// 分块读取并上传
long offset = 0;
using var fileStream = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
var buffer = new byte[blockSize];
int bytesRead;

while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
    using var blockStream = new MemoryStream(buffer, 0, bytesRead);
    await fileClient.UploadRangeAsync(
        new HttpRange(offset, bytesRead),
        blockStream);
    
    offset += bytesRead;
}

// 验证上传完整性
var properties = await fileClient.GetPropertiesAsync();
if (properties.Value.ContentLength == fileInfo.Length)
{
    Console.WriteLine("大文件上传完成");
}

关键注意事项

  • 分块大小必须在4MB到100MB之间,单个文件最多支持1,000,000个块(对应最大100TB文件)。
  • 如需断点续传,可调用fileClient.GetRangesAsync()获取已上传的块范围,跳过已完成的块继续上传。
  • 旧的Azure Data Movement Library已停止维护,所有大文件上传场景均应迁移到v12+的Azure.Storage.Files.Shares SDK。

内容的提问来源于stack exchange,提问作者Classic Neil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:35:40