上传大于4M文件时报错,参考文档:https://learn.microsoft.com/zh-cn/troubleshoot/azure/general/request-body-large

 

处理方法:

// See https://aka.ms/new-console-template for more information
using Azure;
using Azure.Storage.Files.Shares;

Console.WriteLine("Hello, World!");





string connectionString = "";

// Name of the share, directory, and file we'll create
string shareName = "sharelabeled";
string dirName = "sample-dir";
string fileName = "sample-file";

// Path to the local file to upload
string localFilePath = @"/Users/seanyu/Downloads/AZ-400T00A-ZH-TrainerHandbook.pdf";

// Get a reference to a share and then create it
ShareClient share = new ShareClient(connectionString, shareName);
//share.Create();

// Get a reference to a directory and create it
ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
//directory.Create();


// Get a reference to our file
ShareFileClient file = directory.GetFileClient(fileName);
// Max. 4MB (4194304 Bytes in binary) allowed
const int uploadLimit = 4194304;

// Upload the file
using (FileStream stream = File.OpenRead(localFilePath))
{
    file.Create(stream.Length);
    if (stream.Length <= uploadLimit)
    {
        file.UploadRange(
        new HttpRange(0, stream.Length),
        stream);
    }
    else
    {
        int bytesRead;
        long index = 0;
        byte[] buffer = new byte[uploadLimit];
        // Stream is larger than the limit so we need to upload in chunks
        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            // Create a memory stream for the buffer to upload
            MemoryStream ms = new MemoryStream(buffer, 0, bytesRead);
            file.UploadRange(new HttpRange(index, ms.Length), ms);
            index += ms.Length; // increment the index to the account for bytes already written
        }
    }
}