在Azure Blob存储v12中未计算的ContentHash [英] ContentHash not calculated in Azure Blob Storage v12

查看:82
本文介绍了在Azure Blob存储v12中未计算的ContentHash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续传奇,这是第一部分: ContentHash是在Azure.Storage.Blobs v12.xx中为null

Continuing the saga, here is part I: ContentHash is null in Azure.Storage.Blobs v12.x.x

花费大量时间进行调试后,我发现了问题的路由原因,总结起来是这样的:

After spending a good amount of time debugging, I found out the route cause of the problem to summarize it sounds like this:

上传blob后,没有计算内容哈希,BlobContentInfoBlobProperties也没有返回内容哈希,我的整个流程都是基于从Azure接收哈希.

After uploading a blob, the content hash was not calculated, nor the BlobContentInfo or BlobProperties were returning content hash and my whole flow is base on receiving the hash from Azure.

我正在使用方法GetBufferlessInputStreamHttpRequest中获取流,如果我上载此流,则即使我进入Azure存储资源管理器,blob的ContentMD5也为空,因此不会计算内容哈希.但是,但是如果我使用InputStreamHttpRequest中获取流,那么一切都会按预期进行.

I am taking the stream from HttpRequest using the method GetBufferlessInputStream and if I upload this stream the content hash is not calculated, even if I go into azure storage explorer, the ContentMD5 of the blob is empty. BUT, but if I take the stream from HttpRequest using InputStream everything works as expected.

您知道为什么会有这种不同的行为吗?并且您知道如何使通过GetBufferlessInputStream方法接收的流接收内容散列.

Do you know why this different behavior? And do you know how to make to receive content hash for streams received by GetBufferlessInputStream method.

所以代码流如下:

var stream = HttpContext.Current.Request.GetBufferlessInputStream(disableMaxRequestLength: true)

var container = _blobServiceClient.GetBlobContainerClient(containerName);

// get blob reference
var blob = container.GetBlockBlobClient(blobPath);

BlobHttpHeaders blobHttpHeaders = null;
if (!string.IsNullOrWhiteSpace(fileContentType))
{
     blobHttpHeaders = new BlobHttpHeaders()
     {
          ContentType = fileContentType,
     };
}

// retry already configured of Azure Storage API
await blob.UploadAsync(stream, httpHeaders: blobHttpHeaders);

return await blob.GetPropertiesAsync();

ContentHash以上的代码段中未计算出,但如果我更改方式,则使用以下代码段ContentHash从http请求中获取流.

In the code snippet from above ContentHash is NOT calculated, but if I change the way I am getting the stream from the http request with following snippet ContentHash is calculated.

var stream = HttpContext.Current.Request.InputStream

P.S.我认为这很明显,但是对于旧的sdk,内容哈希是针对GetBufferlessInputStream方法接收的流计算的

P.S. I think its obvious, but with the old sdk, content hash was calculated for streams received by GetBufferlessInputStream method

P.S2您还可以在github上找到一个未解决的问题: https://github.com/Azure/azure-sdk-for-net/issues/14037

P.S2 you can find also an open issue on github: https://github.com/Azure/azure-sdk-for-net/issues/14037

P.S3添加了代码片段

P.S3 added code snipet

推荐答案

一种解决方法是,当通过GetBufferlessInputStream()方法获取流时,将其转换为MemoryStream,然后上传MemoryStream.然后,它可以生成contenthash.示例代码如下:

A workaround is that when get the stream via GetBufferlessInputStream() method, convert it to MemoryStream, then upload the MemoryStream. Then it can generate the contenthash. Sample code like below:

        var stream111 = System.Web.HttpContext.Current.Request.GetBufferlessInputStream(disableMaxRequestLength: true);
        //convert to memoryStream.
        MemoryStream stream = new MemoryStream();
        stream111.CopyTo(stream);
        stream.Position = 0;

        //other code
        // retry already configured of Azure Storage API
        await blob.UploadAsync(stream, httpHeaders: blobHttpHeaders);

不知道为什么,但是根据我的调试,我可以看到在最新的SDK中使用方法GetBufferlessInputStream()时,在上传过程中,它实际上调用了此处以获取详细信息.)截图如下:

Not sure why, but as per my debug, I can see when using the method GetBufferlessInputStream() in the latest SDK, during upload, it actually calls the Put Block api in the backend. And in this api, MD5 hash is not stored with the blob(Refer to here for details.). Screenshot as below:

但是,在使用InputStream时,它会调用放入Blob api.截图如下:

However, when using InputStream, it calls the Put Blob api. Screenshot as below:

这篇关于在Azure Blob存储v12中未计算的ContentHash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆