无法将文件上传到 Azure 文件共享

Cannot upload file to azure file share

我正在尝试将 .msg 文件上传到 Azure 文件共享。我关注了 Azure Storage File Shares client library for .NET

我的代码

var connection = "DefaultEndpointsProtocol=https;AccountName=xxxx;EndpointSuffix=core.windows.net";
var shareName = "myfileshare";              
var fileName = "Test details.msg";
var localFilePath = @"C:\Users\xxxx\Desktop\sample\Test details.msg";

var brand = "ABC";
var year = "2021";
var month = "January";
var emailDirection = "Inbound";

ShareClient share = new ShareClient(connection, shareName);

ShareDirectoryClient brandDirectoryClient = share.GetDirectoryClient(brand);               

var yearDirectoryClient = brandDirectoryClient.GetSubdirectoryClient(year);
yearDirectoryClient.CreateIfNotExists();

var monthDirectoyClinet = yearDirectoryClient.GetSubdirectoryClient(month);
monthDirectoyClinet.CreateIfNotExists();

var mailDirectoryClient = monthDirectoyClinet.GetSubdirectoryClient(emailDirection);
mailDirectoryClient.CreateIfNotExists();

// Get a reference to a file and upload it
ShareFileClient file = mailDirectoryClient.GetFileClient(fileName);
using (FileStream stream = File.OpenWrite(localFilePath))

{
    file.Create(stream.Length);
    var result  = file.UploadRange(new HttpRange(0, stream.Length), stream);
}

我正在使用 Azure.Storage.Files.Shares 版本 12.8.0 但是我得到以下异常

消息:请求已中止:请求已取消。 6 次尝试后重试失败。可以在 ClientOptions.Retry.

中调整重试设置

堆栈跟踪

   at Azure.Core.Pipeline.RetryPolicy.<ProcessAsync>d__11.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Azure.Core.Pipeline.RetryPolicy.Process(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelinePolicy.ProcessNext(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.Process(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelinePolicy.ProcessNext(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.Process(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelinePolicy.ProcessNext(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.Process(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelinePolicy.ProcessNext(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipelineSynchronousPolicy.Process(HttpMessage message, ReadOnlyMemory`1 pipeline)
   at Azure.Core.Pipeline.HttpPipeline.Send(HttpMessage message, CancellationToken cancellationToken)
   at Azure.Storage.Files.Shares.FileRestClient.UploadRange(String range, ShareFileRangeWriteType fileRangeWrite, Int64 contentLength, Nullable`1 timeout, Byte[] contentMD5, Stream optionalbody, ShareFileRequestConditions leaseAccessConditions, CancellationToken cancellationToken)
   at Azure.Storage.Files.Shares.ShareFileClient.<UploadRangeInternal>d__95.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Azure.Storage.Files.Shares.ShareFileClient.UploadRange(HttpRange range, Stream content, Byte[] transactionalContentHash, IProgress`1 progressHandler, ShareFileRequestConditions conditions, CancellationToken cancellationToken)
   at xxx.UploadFileToShare(String connection, String shareName, String dirName, String fileName, String localFilePath) in C:\GitRepository\xxxx 100

我正在按照以下代码将 .msg 文件更新为文件共享。

string shareName = "test-share";
string fileName = "testfile";
var brand = "ABC";
var year = "2021";
var month = "January";
var emailDirection = "Inbound";

//// Path to the local file to upload
string localFilePath = @"C:\Users\XXX\Sample MSG File.msg";

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

ShareDirectoryClient brandDirectoryClient = share.GetDirectoryClient(brand);
brandDirectoryClient.CreateIfNotExists();

var yearDirectoryClient = brandDirectoryClient.GetSubdirectoryClient(year);
yearDirectoryClient.CreateIfNotExists();

var monthDirectoyClinet = yearDirectoryClient.GetSubdirectoryClient(month);
monthDirectoyClinet.CreateIfNotExists();

var mailDirectoryClient = monthDirectoyClinet.GetSubdirectoryClient(emailDirection);
mailDirectoryClient.CreateIfNotExists();

ShareFileClient file = mailDirectoryClient.GetFileClient(fileName);

using (FileStream stream = File.OpenRead(localFilePath))
{
    file.Create(stream.Length);
    file.UploadRange(
    new HttpRange(0, stream.Length),
    stream);
}

以同样的方式,您可以使用文件共享进行多项操作。