无法上传预压缩的 brotli Json

Unable to upload brotli precompressed Json

我对c# streams了解甚少。我正在尝试将压缩的 brotli json 上传到 azure 存储中。

private async Task UploadJSONAsync(BlobClient blob, object serializeObject, CancellationToken cancellationToken)
{
  var json = JsonConvert.SerializeObject(serializeObject);
  using (var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  using (var destStream = new MemoryStream())
  using (var brotliStreamCompressor = new BrotliStream(destStream, CompressionLevel.Optimal, false))
  {
    sourceStream.CopyTo(brotliStreamCompressor);
    //brotliStreamCompressor.Close();  // Closes the stream, can't read from a closed stream.

    await blob.DeleteIfExistsAsync();
    await blob.UploadAsync(destStream, cancellationToken);

    //brotliStreamCompressor.Close();  // destStream has zero bytes
    }
  }
}

我确定我缺乏流知识导致无法正常工作。

为了读取流,我必须将其位置设置回零。

private async Task UploadJSONAsync(BlobClient blob, object serializeObject, CancellationToken cancellationToken)
{
  var json = JsonConvert.SerializeObject(serializeObject);
  using (var sourceStream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
  using (var destStream = new MemoryStream())
  using (var brotliStreamCompressor = new BrotliStream(destStream, CompressionLevel.Optimal, false))
  {
    sourceStream.CopyTo(brotliStreamCompressor);
    brotliStreamCompressor.Close();  
    destStream.Position = 0;

    await blob.DeleteIfExistsAsync();
    await blob.UploadAsync(destStream, cancellationToken);

    }
  }
}