将blob上载到azure - 创建身份验证标头

时间:2015-02-06 09:10:11

标签: rest azure

我正在尝试将blob上传到我的azure mediaserver我正在使用以下代码进行上传

   var uploadurl = asset["BaseUri"].ToString() + "/tom.mp4" + asset["ContentAccessComponent"].ToString();

        var formcontent = new MultipartFormDataContent();

        FileStream stream = File.OpenRead(@"C:\tom.mp4");
        byte[] fileBytes = new byte[stream.Length];
        Int32 blobLength = fileBytes.Length;
        stream.Read(fileBytes, 0, fileBytes.Length);
        stream.Close();
        var streamcontent = new StreamContent(new MemoryStream(fileBytes));
        formcontent.Add(streamcontent, "tom.mp4", "tom.mp4");
        formcontent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

        var date = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
        string authorizationHeader = this.Create(date, "mediasvc01f17vpm97tcf", AssetUri.ToString());
        var container = AssetID.ToString();
        container = container.Replace("nb:cid:UUID:", "asset-");

        string auth = this.strTosign(
            date.ToString(CultureInfo.InvariantCulture),
            "BlockBlob",
            "2014-02-14",
            blobLength.ToString(),
            "mediasvc01f17vpm97tcf",
            container,
            "tom.mp4");

        client = new HttpClient();
        client.DefaultRequestHeaders.Add("Authorization", auth);
        client.DefaultRequestHeaders.Add("x-ms-version", "2014-02-14");
        client.DefaultRequestHeaders.Add("x-ms-date", date);
        client.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");

但是我的身份验证不正确我得到了403.任何人都可以给我一个用于将视频blob上传到资产的身份验证标头的示例。

请不要简单地链接到microsoft文档,如果我完全理解它,我不会在这里问:)

我正在尝试像这样创建我的身份验证标头

  private string strTosign(string date,string blobType,string storageversion, string bloblength, string account, string assetname,string blobname)
        {
            var urlPath = String.Format("{0}/{1}", assetname, blobname);

            String canonicalizedHeaders = String.Format(
           "x-ms-blob-type:{0}\nx-ms-date:{1}\nx-ms-version:{2}",
           blobType,
           date,
           storageversion);
            String canonicalizedResource = String.Format("/{0}/{1}", account, urlPath);
            String stringToSign = String.Format(
                    "{0}\n\n\n{1}\n\n\n\n\n\n\n\n\n{2}\n{3}",
                    "PUT",
                    bloblength,
                    canonicalizedHeaders,
                    canonicalizedResource);
            return authhead(stringToSign,account);
        }

        private string authhead(string strignToSign, string _account)
        {
            var sharedKey = Convert.FromBase64String("secret_key");
var hasher = new HMACSHA256(sharedKey);
            var convert = hasher.ComputeHash(Encoding.UTF8.GetBytes(strignToSign));
            string authorizationHeader = string.Format("SharedKey {0}:{1}", _account, Convert.ToBase64String(convert));
            return authorizationHeader;
        }

Fist我按照azure tutorial的例子创建一个字符串,然后我进行加密。但我做错了,因为它一直说认证格式错误。

我的canonicalizedResource看起来像/mediasvc01f17vpm97tcf/asset-09f20ee8-e100-42be-8d20-e921a8c8fdb2/tom.mp4 我的规范化的头像看起来像这样

 x-ms-blob-type:BlockBlob
x-ms-date:Fri, 06 Feb 2015 11:53:10 GMT
x-ms-version:2014-02-14

加密前的整个字符串如下所示

PUT
383631
x-ms-blob-type:BlockBlob
x-ms-date:Fri, 06 Feb 2015 11:53:10 GMT
x-ms-version:2014-02-14
/mediasvc01f17vpm97tcf/asset-09f20ee8-e100-42be-8d20-e921a8c8fdb2/tom.mp4

整个事情就像这样结束了

SharedKey mediasvc01f17vpm97tcf:l3fzhrHP3ab+Ae2uQDO8/4iZkN61umMFp8Dx+od+m/A=

这是我回来的答案

<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidAuthenticationInfo</Code><Message>Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:67382196-0001-0027-0eb7-53bd86000000
Time:2015-02-06T13:10:39.7486266Z</Message></Error>

1 个答案:

答案 0 :(得分:1)

我正在为媒体服务器使用azure服务,这个流程为我提供了一个上传网址。此URL不需要身份验证,这意味着当删除身份验证标头时,它将作为一个魅力。棘手的部分是当我提供身份验证标头时,只是给出了错误格式的错误,而不是说它不需要。

这可能是一个新手的错误,但谢谢你的帮助。