question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Passing BinaryData to BlobClient.UploadAsync() results in NullReferenceException when also using ClientSideEncryptionOptions

See original GitHub issue

Library name and version

Azure.Storage.Blobs 12.10.0.0

Describe the bug

FAILS: Uploading with string content wrapped in BinaryData.

string content = "foo-bar";
BlobClient client;
// not shown here - create BlobClient using SpecializedBlobClientOptions and ClientSideEncryption
await client.UploadAsync(new BinaryData(content), true); // <-- explodes violently, and with its last breath exclaims "Object reference not set to an instance of an object."

Stack trace:

   at Azure.Storage.Blobs.BlobClient+<StagedUploadInternal>d__41.MoveNext (Azure.Storage.Blobs, Version=12.10.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Azure.Storage.Blobs.BlobClient+<UploadAsync>d__37.MoveNext (Azure.Storage.Blobs, Version=12.10.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)

WORKAROUND: Convert BinaryData to Stream.

await client.UploadAsync(new BinaryData(content).ToStream(), true);

OTHER: When not using ClientSideEncryptionOptions this issue does not occur.

Expected behavior

Result of passing BinaryData is equal to result of passing Stream (blob is uploaded successfully).

Actual behavior

NullReferenceException is thrown in the face of an otherwise happy developer.

Reproduction Steps

// running in app service

// common
var connectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage");

// for blob encryption
var credential = new Azure.Identity.ManagedIdentityCredential(Environment.GetEnvironmentVariable("APP_IDENTITY_ID"));
var keyResolver = new Azure.Security.KeyVault.Keys.Cryptography.KeyResolver(credential);
var key = keyResolver.Resolve(new Uri(Environment.GetEnvironmentVariable("VAULT_KEY_URI")));
var encryptionOptions = new Azure.Storage.ClientSideEncryptionOptions(Azure.Storage.ClientSideEncryptionVersion.V1_0)
{
    KeyEncryptionKey = key,
    KeyResolver = keyResolver,
    KeyWrapAlgorithm = Environment.GetEnvironmentVariable("VAULT_KEY_WRAP_ALGORITHM")
};

// get encrpyted blob client
var options = new Azure.Storage.Blobs.Specialized.SpecializedBlobClientOptions() { ClientSideEncryption = encryptionOptions };
var serviceClient = new Azure.Storage.Blobs.BlobServiceClient(connectionString, options);
var containerClient = serviceClient.GetBlobContainerClient("repro");
containerClient.CreateIfNotExists();
var blobClient = containerClient.GetBlobClient("repro");

// upload
await blobClient.UploadAsync(new BinaryData("TEST"), true);

Environment

Azure Function App (Windows, .NET 6) Visual Studio 2022

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
davidjenkinscommented, Feb 19, 2022

I won’t get a chance to test the beta before the 14 day window closes. But I trust you. 😃

1reaction
PramodValavala-MSFTcommented, Jan 17, 2022

@davidjenkins I believe this has been fixed here - #26019

In the meantime, you can use the workaround mentioned in this comment.

@jaschrep-msft Could you share insights on when the next release of the SDK will be?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use "Azure storage blobs" for POST method in ...
How to get the local file path so I can pass on to the FileStream? You can use code like this: string localFilePath...
Read more >
BlobClient.UploadAsync Method (Azure.Storage.Blobs)
The UploadAsync(BinaryData, BlobUploadOptions, CancellationToken) operation overwrites the contents of the blob, creating a new block blob if none exists.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found