简体   繁体   中英

Cannot upload file to azure file share

I am trying to upload .msg file to azure file share. I followed the Azure Storage File Shares client library for .NET

My code

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);
}

I am using Azure.Storage.Files.Shares version 12.8.0 But I get following exception

Message : The request was aborted: The request was canceled. Retry failed after 6 tries. Retry settings can be adjusted in ClientOptions.Retry.

Stack trace

   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

I am following below code to update the .msg file to file share.

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);
}

在此处输入图片说明

In a same way you can do multiple operation using file share.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM