在我们的项目中,我们使用 Azure 存储 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);
我避免在断言策略如果检查中检查。因此,底线,设置RequireEncryption = false
或将其设置为 true,并定义适当的加密策略。
现在,这使所有上传****异步方法都有效。
这是帮助我调试问题的调用堆栈:
{"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)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.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)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.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)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.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)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.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)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonCoreUtilAsyncExtensions.cs:line 210
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromByteArrayAsync(Byte[] buffer, Int32 index, Int32 count, CancellationToken cancellationToken) in c:Program Files (x86)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.cs:line 1204
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.UploadFromByteArrayAsync(Byte[] buffer, Int32 index, Int32 count) in c:Program Files (x86)Jenkinsworkspacerelease_dotnet_masterLibClassLibraryCommonBlobCloudBlockBlob.cs:line 1190