0

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>
4
  • One thing I noticed is that you're using DateTime.Now (2nd Code Block). Please use DateTime.UtcNow instead. Otherwise the code to calculate your authentication header looks fine to me. Please share the code for uploading (or are you using the same code you included above (1st code block in your question))? Commented Feb 6, 2015 at 12:26
  • Thank you so much for responding. I have updated the code snippets for how I try to upload now. But basically I have changed it to utc and of course updated the Authorization in the header. But I still get the same error Commented Feb 6, 2015 at 12:55
  • Another thing I noticed is that you're setting the content-type to application/octet-stream in the request however it is not set in when you're creating the stringToSign. This could also cause authorization header mismatch. Please include content type in your stringToSign as well. Commented Feb 6, 2015 at 13:22
  • Apparently I was wrong. With the flow I am using I do not need to authenticate at all. From the service I get an upload url and this do not require authentication. Commented Feb 9, 2015 at 8:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.