Azure 存储的断点续传与 MD5 校验

时间:2021-07-15 | 标签: | 作者:Q8 | 来源:Microsoft Azure网络

小提示:您能找到这篇{Azure 存储的断点续传与 MD5 校验}绝对不是偶然,我们能帮您找到潜在客户,解决您的困扰。如果您对本页介绍的Azure 存储的断点续传与 MD5 校验内容感兴趣,有相关需求意向欢迎拨打我们的服务热线,或留言咨询,我们将第一时间联系您!

< ">

< ">问题分析



< ">首先关于 Azure 存储中 MD5 的描述,我们已经有相关的介绍文档,如果对于存储中 MD5 的描述不熟悉,可以先参考 Azure Blob 存储基于 MD5 的完整性检查的内容。

< ">如果直接将文件上传到 Blob 中可以在上传的方法中配置 BlobRequestOptions 类,将该类的 StoreBlobContentMD5 参数设置为 true,即可在上传时自动计算 MD5 值并将此值写入到请求头部(Content-MD5)中(可以参考 BlobRequestOptions.StoreBlobContentMD5 Property 此文档的描述)。 但是如果使用断点续传的方法,是将文件分为多个块上传,之后通过 PubBlockList 请求完成组合,那么想要上传 MD5 值,需要在 PubBlockList 请求的头部添加 x-ms-blob-content-md5 参数,但是在 sdk 相关的方法中,BlobRequestOptions 中并没有关于该参数的属性,所以如果使用断点续传,采用 sdk 的 PubBlockList() 方法无法将 MD5 值上传上去,本篇文档即要解决如何在断点续传时上传 MD5 值的问题。

< ">解决方案

< ">可以通过使用 REST API 的方式来解决此问题:

< ">1.首先我们需要计算出文件的 MD5 值:

< ">string contentHash = md5()(File.ReadAllBytes(sourcePath));

< ">2.将文件分块上传:

< ">public async Task PutBlobAsync(String containerName, String blobName, byte[] blobContent, String blobid, bool error = false)

< ">{

< ">    String requestMethod = "PUT";

< ">    String urlPath = String.Format("{0}/{1}", containerName, blobName) + "?comp=block&blockid=" + blobid;

< ">    String storageServiceVersion = "2015-02-21";

< ">    String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);


< ">    Int32 blobLength = blobContent.Length;

< ">    //headers

< ">    String canonicalizedHeaders = String.Format(

< ">        " x-ms-date:{0} x-ms-version:{1}",

< ">        dateInRfc1123Format,

< ">        storageServiceVersion);

< ">    //resources

< ">    String canonicalizedResource = String.Format("/{0}/{1}", AzureConstants.Account, String.Format("{0}/{1}", containerName, blobName) + " blockid:" + blobid + " comp:block");

< ">    String stringToSign = String.Format(

< ">    "{0} {1} {2} {3}",

< ">    requestMethod,

< ">    blobLength,

< ">    canonicalizedHeaders,

< ">    canonicalizedResource);

< ">    string authorizationHeader = CreateAuthorizationHeader(stringToSign);



< ">    //上传url

< ">    Uri uri = new Uri(BlobEndPoint + urlPath);

< ">    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

< ">    request.Method = requestMethod;

< ">    request.Headers["x-ms-date"] = dateInRfc1123Format;

< ">    request.Headers["x-ms-version"] = storageServiceVersion;

< ">    request.Headers["Authorization"] = authorizationHeader;

< ">    request.ContentLength = blobLength;


< ">    try {

< ">        using (Stream requestStream = await request.GetRequestStreamAsync()) {

< ">            requestStream.Write(blobContent, 0, blobLength);

< ">        }


< ">        using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) {

< ">            String ETag = response.Headers["ETag"];

< ">            System.Console.WriteLine(ETag);

< ">        }

< ">        error = false;

< ">    }

< ">    catch (WebException ex) {

< ">        System.Console.WriteLine("An error occured. Status code:" + ((HttpWebResponse)ex.Response).StatusCode);

< ">        System.Console.WriteLine("Error information:");

< ">        error = true;

< ">        using (Stream stream = ex.Response.GetResponseStream()) {

< ">            using (StreamReader sr = new StreamReader(stream)) {

< ">                var s = sr.ReadToEnd();

< ">                System.Console.WriteLine(s);

< ">            }

< ">        }

< ">    }

< ">}

< ">3.在 PutBlobListAsync() 方法中将 MD5 值和 x-ms-blob-content-md5 写入到请求头中:

< ">public async Task PutBlobListAsync(String containerName, String blobName, List<string> blobIdList, string md5, bool error = false)

< ">{

< ">    String requestMethod = "PUT";

< ">    String urlPath = String.Format("{0}/{1}", containerName, blobName) + "?comp=blocklist";

< ">    String storageServiceVersion = "2015-02-21";

< ">    String dateInRfc1123Format = DateTime.UtcNow.ToString("R", CultureInfo.InvariantCulture);


< ">    String canonicalizedHeaders = String.Format(

< ">        " x-ms-blob-content-md5:{0} x-ms-date:{1} x-ms-version:{2}",

< ">        md5,

< ">        dateInRfc1123Format,

< "> 软文推广软文营销新闻源发;       storageServiceVersion);

< ">    StringBuilder stringbuilder = new StringBuilder();

< ">    stringbuilder.Append("<BlockList>");

< ">    foreach (string item in blobIdList) {

< ">        stringbuilder.Append(" <Latest>" + item + "</Latest>");

< ">    }

< ">    stringbuilder.Append("</BlockList>");


< ">    byte[] data = Encoding.UTF8.GetBytes(stringbuilder.ToString());


< ">    Int32 blobLength = data.Length;

< ">    String canonicalizedResource = String.Format("/{0}/{1}", AzureConstants.Account, String.Format("{0}/{1}", containerName, blobName) + " comp:blocklist");

< ">    String stringToSign = String.Format(

< ">        "{0} {1} {2} {3}",

< ">   服务器cdn加速;     requestMethod,

< ">        blobLength,

< ">        canonicalizedHeaders,

< ">        canonicalizedResource);

< ">    String authorizationHeader = CreateAuthorizationHeader(stringToSign);


< ">    Uri uri = new Uri(BlobEndPoint + urlPath);

< ">    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

< ">    request.Method = requestMethod;

< ">    request.Headers["x-ms-blob-content-md5"] = md5;

< ">    request.Headers["x-ms-date"] = dateInRfc1123Format;

< ">    request.Headers["x-ms-version"] = storageServiceVersion;

< ">    request.Headers["Authorization"] = authorizationHeader;

< ">    request.ContentLength = blobLength;

< ">    try {

< ">        using (Stream requestStream = await request.GetRequestStreamAsync()) {

< ">            requestStream.Write(data, 0, blobLength);

< ">        }


< ">        using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync()) {

< ">            String ETag = response.Headers["ETag"];

< ">            System.Console.WriteLine(ETag);

< ">        }

< ">        error = false;

< ">    }

< ">    catch (WebException ex) {

< ">        System.Console.WriteLine("An error occured. Status code:" + ((HttpWebResponse)ex.Response).StatusCode);

< ">        System.Console.WriteLine("Error information:");

< ">        error = true;

< ">        using (Stream stream = ex.Response.GetResponseStream()) {

< ">            using (StreamReader sr = new StreamReader(stream)) {

< ">                var s = sr.ReadToEnd();

< ">                System.Console.WriteLine(s);

< ">            }

< ">        }

< ">    }

< ">}

< ">完整示例请参考示例代码。

Azure 存储的断点续传与 MD5 校验

上一篇:YouTube营销,跨境卖家不可错过的广告套路!
下一篇:Azure:为云服务创建内部负载均衡器


版权声明:以上主题为“Azure 存储的断点续传与 MD5 校验"的内容可能是本站网友自行发布,或者来至于网络。如有侵权欢迎联系我们客服QQ处理,谢谢。
相关内容
推荐内容
扫码咨询
    Azure 存储的断点续传与 MD5 校验
    打开微信扫码或长按识别二维码

小提示:您应该对本页介绍的“Azure 存储的断点续传与 MD5 校验”相关内容感兴趣,若您有相关需求欢迎拨打我们的服务热线或留言咨询,我们尽快与您联系沟通Azure 存储的断点续传与 MD5 校验的相关事宜。

关键词:Azure,存储的断点续传与

关于 | 业务 | 案例 | 免责 | 隐私
客服邮箱:sales@1330.com.cn
电话:400-021-1330 | 客服QQ:865612759
沪ICP备12034177号 | 沪公网安备31010702002418号