使用 Azure 存储异步创建 blob 时出现异常 - UploadTextAsync
Exception when creating blob asyncronously with Azure Storage - UploadTextAsync
在我们的项目中,我们使用的是 Azure Storage 6.0.0 版本,我们正在使用 UploadTextAsync
方法来创建 blob。更新到最新版本后,UploadTextAsync 函数停止工作并抛出异常:
"Object reference not set to an instance of an object."
我尝试了不同的方法来替换它,但都抛出相同的异常。例如:
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);
如何使异步版本工作?有解决方法吗?我真的需要异步版本。
如果 blob 不存在,UploadText(异步和同步)方法会创建它,如果存在则覆盖它。
为什么它适用于同步版本而不适用于更新的异步版本?
对于创建 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
在我们的项目中,我们使用的是 Azure Storage 6.0.0 版本,我们正在使用 UploadTextAsync
方法来创建 blob。更新到最新版本后,UploadTextAsync 函数停止工作并抛出异常:
"Object reference not set to an instance of an object."
我尝试了不同的方法来替换它,但都抛出相同的异常。例如:
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);
如何使异步版本工作?有解决方法吗?我真的需要异步版本。
如果 blob 不存在,UploadText(异步和同步)方法会创建它,如果存在则覆盖它。
为什么它适用于同步版本而不适用于更新的异步版本?
对于创建 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