Uploading blob to azure - Create authentication he

2019-09-13 01:05发布

问题:

I am trying to upload a blob to my azure mediaserver I am using the following code to do the upload

   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");

However My authentication is not correct I get a 403. Can anyone give me an example of a authentication header for uploading a video blob to an asset.

Please do not simply link to the microsoft documentation, if I understood them fully I would not ask here :)

I am trying to create my authentication header like this

  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 I create a string following the example of azure tutorial and the I do the encryption. But I am doing something wrong because it keeps saysing the authentication is in wrong format.

My canonicalizedResource looks like this /mediasvc01f17vpm97tcf/asset-09f20ee8-e100-42be-8d20-e921a8c8fdb2/tom.mp4 My canonicalizedHeaders look like this

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

the whole string before encryption look like this

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

And the whole thing ends up like this

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

this is the answer i get back

<?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:

I am using the azure service for a media server and this flow provides me with an upload url. This url do not require an authentication, which means that when the authentication header was removed it worked as a charm. The tricksy part was when I provided an authentication header is just gave an error of wrong format instead of saying it was not needed.

It is probably a rookie mistake, but thank you for the help.



标签: rest azure