Search code examples
c#.netazure-files.net-4.6.1azure-storage-files

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

Solution

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

    enter image description here

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