2

I'm trying to upload large files to Azure File Share via the Azure.Storage.Files.Shares library and am I running into corruption issues on all media files (images, PDFs, etc) over ~4 MB. Azure File Share has a limit of 4 MB for a single request which is why I've split the upload in to multiple chunks, but it still corrupts the files despite every chunk upload returning a 201.

Notes:

  • It doesn't seem like it's an issue with having to write multiple chunks as I can write a 3 MB file in as many chunks as I want and it will be totally fine
  • .txt files over 4 MB have no issues and display totally fine after uploading

This uploading portion of this function is basically copied/pasted from the only other stack overflow "solution" I found regarding this issue:

public async Task WriteFileFromStream(string fullPath, MemoryStream stream)
{
    // Get pieces of path
    string dirName = Path.GetDirectoryName(fullPath);
    string fileName = Path.GetFileName(fullPath);
    
    ShareClient share = new ShareClient(this.ConnectionString, this.ShareName);

    // Set position of the stream to 0 so that we write all contents
    stream.Position = 0;
    try
    {
        // Get a directory client for specified directory and create the directory if it doesn't exist
        ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
        directory.CreateIfNotExists();

        if (directory.Exists())
        {
            // Get file client
            ShareFileClient file = directory.GetFileClient(fileName);
            
            // Create file based on stream length
            file.Create(stream.Length);

            int blockSize = 300 * 1024; // can be anything as long as it doesn't exceed 4194304
            long offset = 0; // Define http range offset
            BinaryReader reader = new BinaryReader(stream);

            while (true)
            {
                byte[] buffer = reader.ReadBytes(blockSize);
                if (buffer.Length == 0)
                    break;

                MemoryStream uploadChunk = new MemoryStream();
                uploadChunk.Write(buffer, 0, buffer.Length);
                uploadChunk.Position = 0;

                HttpRange httpRange = new HttpRange(offset, buffer.Length); // offset -> buffer.Length-1 (inclusive)
                var resp = file.UploadRange(httpRange, uploadChunk);
                
                Console.WriteLine($"Wrote bytes {offset}-{offset+(buffer.Length-1)} to {fullPath}. Response: {resp.GetRawResponse()}");
                offset += buffer.Length;    // Shift the offset by number of bytes already written
            }

            reader.Close();
        }
        else
        {
            throw new Exception($"Failed to create directory: {dirName}");
        }
    }
    catch (Exception e)
    {
        // Close out memory stream
        throw new Exception($"Error occured while writing file from stream: {e.Message}");
    }
}

Any help on this is greatly appreciated.

bamishr
  • 410
  • 1
  • 6
  • 23
ThomasJazz
  • 99
  • 9

0 Answers0