question

NikhilNair-2055 avatar image
0 Votes"
NikhilNair-2055 asked MahataRajib-8416 commented

Uploading Files more than 30 MB to Azure fileshare locaton from FTP using an console application(.net-C#)

I'm using a C# (.net core) console program to upload a large file (.CSV) (limit 30 MB) to an Azure Fileshare location from an FTP site. In some cases, I'm getting incomplete writes in the Azure fileshare location. However, it works most of the time when I upload in chunks. Due to incomplete writing, the file might get damaged at the destination Fileshare location (missing a few rows and contents from the source location). The code I use is shown below. Please help me improve the procedure or provide other solutions.

 public async void WriteFileToFileShare(ShareClient share, string azureFolder, string validDataFilePath, Stream stream)
         {
             ShareDirectoryClient directory = share.GetDirectoryClient(azureFolder);
    
             //  Azure allows for 4MB max uploads  (4 x 1024 x 1024 = 4194304)
             const int uploadLimit = 4194304;
             stream.Seek(0, SeekOrigin.Begin);   // ensure stream is at the beginning
             var fileClient = await directory.CreateFileAsync(Path.GetFileName(validDataFilePath), stream.Length);
             // If stream is below the limit upload directly
             if (stream.Length <= uploadLimit)
             {
                 await fileClient.Value.UploadRangeAsync(new HttpRange(0, stream.Length), stream);
                 return;
             }
             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
                 using MemoryStream ms = new MemoryStream(buffer, 0, bytesRead);
                 await fileClient.Value.UploadRangeAsync(ShareFileRangeWriteType.Update, new HttpRange(index, ms.Length), ms);
                 index += ms.Length; // increment the index to the account for bytes already written
             }
         }
azure-filesdotnet-cli
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@NikhilNair-2055 Welcome to Microsoft Q&A Forum, Thank you posting your query here! I am checking on this thread

0 Votes 0 ·

I also facing the same issue on my .docx upload in the file share. Use file stream rather than memory stream to upload the file. It's worked for me. Example:

using (FileStream fileStream = File.OpenRead(mergeDocTempPath))
{
WriteFileToFileShare(shareClient , azureFolder, validDataFilePath, fileStream )
}

0 Votes 0 ·

0 Answers