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

Posted

技术标签:

【中文标题】无法将文件上传到 Azure 文件共享【英文标题】:Cannot upload file to azure file share 【发布时间】:2021-12-05 15:28:20 【问题描述】:

我正在尝试将 .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

【问题讨论】:

【参考方案1】:

我正在按照以下代码将.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);

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

【讨论】:

您好,我不确定这与我目前使用的代码有何不同? 我使用的代码与您使用的相同,而不是 File.OpenWrite 我使用的是 File.OpenRead。 完美,成功了

以上是关于无法将文件上传到 Azure 文件共享的主要内容,如果未能解决你的问题,请参考以下文章

当文件上传到 azure 文件共享时,如何添加触发器以将文件从 azure 文件共享移动到 azure blob?

将 base64 上传到 azure 文件共享

我们可以在 .net core 3.1 中一次性上传多少大文件到 Azure 文件共享

如何在 Azure 存储文件共享上上传大文件?

文件上传到 azure 文件共享不适用于更大的文件

无法将文件上传到 Azure Blob Node.js