title: C# Azure upload file error The specified blob or block content is invalid
body: <p>I'm uploading files to azure storage.</p>
  
  <pre><code>public class AzureBlob : ICloudBlob
  {
      private string _fileName;
      public string FileName
      {
          get =&gt; _fileName;
          set
          {
              _fileName = value;
              _cloudBlockBlob = CloudBlobContainer.GetBlockBlobReference(value);
          }
      }
      public CloudBlobContainer CloudBlobContainer { get; set; }
      private CloudBlockBlob _cloudBlockBlob;
  
      public async Task UploadChunksFromPathAsync(string path, long fileLength)
      {
          const int blockSize = 256 * 1024;
          var bytesToUpload = fileLength;
          long bytesUploaded = 0;
          long startPosition = 0;
  
          var blockIds = new List&lt;string&gt;();
          var index = 0;
  
          do
          {
              var bytesToRead = Math.Min(blockSize, bytesToUpload);
              var blobContents = new byte[bytesToRead];
  
              using (var fs = new FileStream(path, FileMode.Open))
              {
                  fs.Position = startPosition;
                  fs.Read(blobContents, 0, (int) bytesToRead);
              }
  
              var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(index.ToString("d6")));
  
              blockIds.Add(blockId);
              await _cloudBlockBlob.PutBlockAsync(blockId, new MemoryStream(blobContents), null);
  
              bytesUploaded += bytesToRead;
              bytesToUpload -= bytesToRead;
              startPosition += bytesToRead;
              index++;
          } while (bytesToUpload &gt; 0);
  
          await _cloudBlockBlob.PutBlockListAsync(blockIds);   
      }
  }
  </code></pre>
  
  <p>This works fine for one file upload, multiple file uploads calling this method one after the other throws a 400 error on <code>_cloudBlockBlob.PutBlockListAsync</code> with the azure error being </p>
  
  <blockquote>
    <p>The specified blob or block content is invalid.</p>
  </blockquote>
  
  <p><strong>If I remove the await keyword on <code>_cloudBlockBlob.PutBlockListAsync</code> it works completely fine.</strong></p>
  
  <p>The blockIds are all of the same length. What am I doing wrong?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Calling code in controller:</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;IActionResult&gt; Upload([FromBody] UploadViewModel model)
  {
      var audioBlob = _cloudStorage.GetBlob(CloudStorageType.Audio, model.AudioName);
  
      await audioBlob.UploadChunksFromPathAsync(model.AudioPath, model.FileLength);
  
      return Ok();
  }
  </code></pre>
  
  <p>Storage:</p>
  
  <pre><code>public enum CloudStorageType
  {
      Audio,
      Image,
  }
  
  public class AzureStorage : ICloudStorage
  {
      public IDictionary&lt;CloudStorageType, ICloudBlob&gt; CloudBlobs { get; set; }
  
      public AzureStorage(IConfiguration configuration)
      {
          var storageAccount = CloudStorageAccount.Parse(configuration["ConnectionStrings:StorageConnectionString"]);
          var blobClient = storageAccount.CreateCloudBlobClient();
  
          CloudBlobs = new Dictionary&lt;CloudStorageType, ICloudBlob&gt;();
  
          foreach (CloudStorageType cloudStorageType in Enum.GetValues(typeof(CloudStorageType)))
          {
              CloudBlobs[cloudStorageType] = new AzureBlob(cloudStorageType.ToString().ToLower(), blobClient);
          }
      }
  
      public ICloudBlob GetBlob(CloudStorageType cloudStorageType, string fileName)
      {
          CloudBlobs[cloudStorageType].FileName = fileName;
  
          return CloudBlobs[cloudStorageType];
      }
  }
  </code></pre>
  
  <p>Startup.cs</p>
  
  <pre><code>var azureStorage = new AzureStorage(_configuration);
  
  // Add application services.
  services.AddSingleton(_configuration);
  services.AddSingleton&lt;ICloudStorage&gt;(azureStorage);
  </code></pre>