在Azure.Storage.Blobs v12.x.x中ContentHash为空 [英] ContentHash is null in Azure.Storage.Blobs v12.x.x
问题描述
我正在尝试将我的项目从 Microsoft.WindowsAzure.Storage v9(不建议使用)升级到最新的SDK Azure.Storage.Blobs v12.
I am trying to upgrade my project from Microsoft.WindowsAzure.Storage v9 (deprecated) to latest sdk Azure.Storage.Blobs v12.
我的问题(升级后)是访问 ContentHash 属性.
My issue (post-upgrade) is accessing the ContentHash property.
升级前步骤:
- 将文件上传到blob
- 从 Microsoft.WindowsAzure.Storage.Blob 获取由
- 将计算出的MD5哈希值与从azure检索到的哈希值进行比较
CloudBlob.Properties.ContentMD5
提供的上传文件的MD5哈希.- upload file to blob
- get MD5 hash of uploaded file provided by
CloudBlob.Properties.ContentMD5
from Microsoft.WindowsAzure.Storage.Blob - compare the calculated MD5 hash with the one retrieved from azure
升级后尝试访问Azure正在其端计算的MD5哈希:
Post-upgrade attempts to access the MD5 hash that Azure is calculating on its side:
1. BlobClient.GetProperties()
调用此方法
2. BlobClient.UploadAsync()
查看 BlobContentInfo
响应
两者返回 ContentHash 为空.(请参阅后面的问题,以了解原因)
我注意到的一个巨大区别是,使用较旧的sdk,我可以告诉存储客户端使用MD5计算,如下所示:
One huge difference I've noticed is that with older sdk I could tell to the storage client to use MD5 computing like this:
CloudBlobClient cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient();
cloudBlobClient.DefaultRequestOptions.StoreBlobContentMD5 = true;
所以我希望在最新的SDK中找到类似于 StoreBlobContentMD5 的东西,但我找不到.
So I was expecting to find something similar to StoreBlobContentMD5 on the latest sdk but I couldn't.
有人可以帮我找到解决这个问题的方法吗?
Can anyone help me find a solution for this problem?
修改1:我进行了测试,在天蓝色的存储中,我没有 MD5哈希
Edit 1: I did a test and in azure storage I do not have a MD5 hash
上传代码:
var container = _blobServiceClient.GetBlobContainerClient(containerName);
var blob = container.GetBlobClient(blobPath);
BlobHttpHeaders blobHttpHeaders = null;
if (!string.IsNullOrWhiteSpace(fileContentType))
{
blobHttpHeaders = new BlobHttpHeaders()
{
ContentType = fileContentType,
};
}
StorageTransferOptions storageTransferOption = new StorageTransferOptions()
{
MaximumConcurrency = 2,
};
var blobResponse = await blob.UploadAsync(stream, blobHttpHeaders, null, null, null, null, storageTransferOption, default);
return blob.GetProperties();
除了使用新sdk中的新类之外,旧的上传代码与新的上传代码之间没有太大区别.
There is not much difference between old upload code and new one apart from using new classes from new sdk.
主要区别仍然是我已经说过的,我在新的sdk中找不到 StoreBlobContentMD5 的等效设置.我认为这是问题所在.我需要设置存储客户端以计算MD5哈希值,就像处理旧sdk一样.
The main difference remains the one I already stated, I can not find an equivalent setting in new sdk for StoreBlobContentMD5 . I think this is the problem. I need to set the storage client to compute MD5 hash, as I did with old sdk.
修改2:对于下载,我可以执行以下操作:
Edit 2: For download I can do something like this:
var properties = blob.GetProperties();
var download = await blob.DownloadAsync(range: new HttpRange(0, properties.Value.ContentLength), rangeGetContentHash: true);
通过使用DownloadAsync的此定义,我可以强制计算MD5哈希,并且可以在 download.Value.ContentHash
By using this definition of DownloadAsync I can force MD5 hash to be calculated and it can be found in download.Value.ContentHash
推荐答案
总结以关闭问题:
我对最新版本的12.4.4 blob存储包进行了快速测试,可以看到content-md5是自动生成的,也可以读取.
I did a quick test with the latest version of 12.4.4 blob storage package, I can see the content-md5 is auto-generated and can also be read.
根据操作员的评论,这可能是由于现有解决方案存在一些问题.创建新解决方案后,它可以按预期工作.
And as per the op's comment, it may due to some issues with the existing solution. And after creating a new solution, it works as expected.
这篇关于在Azure.Storage.Blobs v12.x.x中ContentHash为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!