gpt4 book ai didi

c# - 使用 Azure 存储异步创建 blob 时出现异常 - UploadTextAsync

转载 作者:行者123 更新时间:2023-11-30 23:02:26 24 4
gpt4 key购买 nike

在我们的项目中,我们使用 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);

如何使异步版本正常工作?有解决方法吗?我真的需要异步版本。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com