在我们的项目中,我们使用的是Azure Storage 6.0.0版本,并且使用UploadTextAsync
方法创建Blob。更新到最新版本后,UploadTextAsync函数停止工作,现在引发异常:
“你调用的对象是空的。”
我尝试了不同的方法来替换它,但是都抛出相同的异常。例如:
byte[] fileBytes = Encoding.UTF8.GetBytes(data);
await lockBlob.UploadFromByteArrayAsync(fileBytes, 0, fileBytes.Length);
使它起作用的唯一方法是更改为同步版本:
BlobRequestOptions blobOptions = new BlobRequestOptions
{
MaximumExecutionTime = TimeSpan.FromSeconds(5),
};
lockBlob.UploadText(data, null, null, blobOptions, null);
如何使异步版本正常工作?有解决方法吗?我真的需要异步版本。
如果不存在,则UploadText(异步和同步)方法将创建该Blob,如果存在,则将其覆盖。
为什么它与同步版本一起使用而不与更新异步?
对于Blob创建,代码如下:
var container= client.GetContainerReference("lockblobs");
await container.CreateIfNotExistsAsync();
var lockBlob = container.GetBlockBlobReference(data);
最佳答案
解决了:
通过Azure包内的调用堆栈进行调试后,事实证明BlobRequestOptions中存在检查,默认情况下,RequireEncryption和EncryptionPolicy之间不匹配。
这是BlobRequestOptions定义内的元凶:
internal void AssertPolicyIfRequired()
{
if (this.RequireEncryption.HasValue && this.RequireEncryption.Value && this.EncryptionPolicy == null)
{
throw new InvalidOperationException(SR.EncryptionPolicyMissingInStrictMode);
}
}
在https://github.com/Azure/azure-storage-net/blob/master/Lib/Common/Blob/BlobRequestOptions.cs中
因此,基本上可以这样做:
BlobRequestOptions blobOptions = new BlobRequestOptions
{
MaximumExecutionTime = TimeSpan.FromSeconds(5),
ParallelOperationThreadCount = 48,
RequireEncryption = false
};
await lockBlob.UploadTextAsync(data, null, null, blobOptions, null);
我避免在AssertPolicyIfRequired检查中进行检查。因此,将底线设置为
RequireEncryption = false
或将其设置为true,并定义适当的加密策略。现在,这使所有Upload **** Async方法都可以工作。
这是帮助我调试问题的调用堆栈:
{"ClassName":"System.NullReferenceException","Message":"Object reference not set to an instance of an object.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<>c__DisplayClass1d.b__f(Action continuation) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 620
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.BeginUploadFromStreamHelper(Stream source, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, AsyncCallback callback, Object state) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 665
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.BeginUploadFromStream(Stream source, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, AsyncCallback callback, Object state) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 477
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.BeginUploadFromByteArray(Byte[] buffer, Int32 index, Int32 count, AsyncCallback callback, Object state) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 1146
at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.TaskFromVoidApm[T1,T2,T3](Func`6 beginMethod, Action`1 endMethod, T1 arg1, T2 arg2, T3 arg3, CancellationToken cancellationToken) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\AsyncExtensions.cs:line 210
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromByteArrayAsync(Byte[] buffer, Int32 index, Int32 count, CancellationToken cancellationToken) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 1204
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromByteArrayAsync(Byte[] buffer, Int32 index, Int32 count) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 1190
关于c# - 与Azure存储异步创建Blob时发生异常-UploadTextAsync,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50477987/