0

I have very little understanding of the c# streams. I'm trying to upload brotli compressed json into azure storage.

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
    }
  }
}

I'm sure my lack of stream knowledge is preventing this from working.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150

1 Answers1

0

In order to read the stream I had to set it position back to zero.

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

    }
  }
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • You don't really need to do `await blob.DeleteIfExistsAsync();`. It's an extra network call. `blob.UploadAsync()` will automatically overwrite an existing blob or create a new blob if it doesn't exist. – Gaurav Mantri Jul 05 '21 at 03:20
  • @GauravMantri [Documentation](https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.blobclient.uploadasync?view=azure-dotnet#Azure_Storage_Blobs_BlobClient_UploadAsync_System_IO_Stream_System_Threading_CancellationToken_) States otherwise, and is my experience: *The UploadAsync(Stream, CancellationToken) operation creates a new block blob or throws if the blob already exists.*. – Erik Philips Jul 05 '21 at 15:49
  • Sorry, my bad! You can use `BlockBlobClient` instead of `BlobClient`. From [`Documentation`](https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.specialized.blockblobclient.uploadasync?view=azure-dotnet#Azure_Storage_Blobs_Specialized_BlockBlobClient_UploadAsync_System_IO_Stream_Azure_Storage_Blobs_Models_BlobUploadOptions_System_Threading_CancellationToken_): *The UploadAsync(Stream, BlobUploadOptions, CancellationToken) operation overwrites the contents of the blob, creating a new block blob if none exists.* – Gaurav Mantri Jul 05 '21 at 15:53
  • 1
    @GauravMantri Also the same method (as I was just looking) without the CancellationToken does overwrite. Yay Inconsistencies!! – Erik Philips Jul 05 '21 at 16:02