Amazon Cloudfront Private Streaming Example

Amazon Cloudfront streaming servers with private URLs have just been online for a few weeks. Therefore there is not much information available nor are there many code examples.

Here are a few links to explaining websites and a working PHP code example.

I hope this helps some other developers :-)

Amazon Documentation
Amazon Forum
Cloudberry HowTo
Bucket Explorer HowTo

>>> Download Code Example:
>>> cloudfront_private_streaming_example.zip

Tags: , , , , ,

17 Responses to “Amazon Cloudfront Private Streaming Example”

  1. Flint says:

    I really appreciate the code that generates the signing but am I bit perplexed as to how this example then plays the content as theres not evident JS. Unfortunately that is where I’m still having problems. I was hoping to use it with Flowplayer but am about to abandon those efforts as it seems very buggy. What do you use as the player for those generated urls?

  2. admin says:

    I use the JW player from http://www.longtailvideo.com/
    They just released a version that accepts URLs for Amazon private streaming. I’m not sure that Flowplayer already has a version that works.

    I included the player in the ZIP file so you can download it again.

  3. Jared says:

    This is awesome! Really helped me get my videos working with private streaming!

    I switched the code from one server to another and it stopped working though…. is your code php5 only perhaps? (Got a “Stream not found” error in the jw player.)

  4. admin says:

    @Jared:
    Good, that others think this is useful! It took me quite some time to find out.

    I didn’t test the code on PHP4 (it’s not supported anymore …). But I don’t think there is any special PHP5 code.

    Maybe OpenSSL is not included? But then you should get an error using the function openssl_get_privatekey.

    Did you check if $urlcloud looks OK?

  5. Jared says:

    Good tip! The signature part of the url was not getting generated. In PHP5 for openssl_sign(), the signature_alg (OPENSSL_ALGO_SHA1) parameter was added. I removed it and it works like a charm!

    You are right about there not being much out there…. I gratefully stumbled onto your blog post after a nearly all night search and digging through Amazon’s API reference to get a private streaming demo working. Thanks again!

  6. Eddie says:

    Thanks a lot ! if you have an update/extra features would be great!

  7. Checkmark says:

    Fantastic!! Your code is very well documented and fairly easy to follow. Thank you so much for providing this excellent help.

  8. Victor says:

    Hi,

    I used your code as a reference and all worked well except for getting the mp3 to play in a jwplayer.

    Here is an example of the player config I use, but it does not play. Any ideas?

    When I put the unsigned version of the same mp3 it does work. I am using the latest version of jwplayer – 5.2

  9. Victor says:

    The code in the embed is:

    type=’application/x-shockwave-flash’
    id=’single2′
    name=’single2′
    src=’player.swf’
    width=’470′
    height=’24′
    bgcolor=’undefined’
    allowscriptaccess=’always’
    allowfullscreen=’true’
    wmode=’transparent’
    flashvars=’file=bunny.mp3%3FExpires%3D1278601480%26Signature%3DSWvtYfiYrKBgSMutFlh4dDA6m1CwWxAzShhnO0NTt56qmLHrO3JYtnEaznZA95xMlOEZdIUZIIqUT80gPEBu5GhodiZztCmbSVwtxKgoVb-F8fv4-3gLp2RscBYUhUOWFt6vkbv9w01B9AQd9X6UjI085fFoOMiBDYuHP1Gm4Zc_%26Key-Pair-Id%3DAPKAIXJUVQ5QFEOUJMDA&streamer=rtmp://s7u5y1j8ivhpl.cloudfront.net/cfx/st/’

  10. Dipen says:

    Guys,

    I tried the example and also i removed the part
    $res = openssl_sign($policy_str, $signature, $pkeyid, OPENSSL_ALGO_SHA1);

    But signature is not generated. Can you please help me to sort it out.

    http://qwikinet.com/demo/chemsmart/new/cloudfront_private_streaming_example.php

  11. admin says:

    You remove one line of code and then you think it should still work? A very strange way of thinking. Of course you need this line of code.

  12. John says:

    Works wonderful, except that the expire time is not respected for some strange reason.
    When you set the expire time to 10 seconds and wait to click on the video until 10 seconds have passed, you normally get an access denied error.
    But not in this case. In fact, it takes a very long time before access is denied.

    I set this: strtotime(“+10 seconds”);
    I tried an alternative: time() + 10;
    Both give the same result, expiring time doesn’t seem to happen in the first hour or so. Perhaps there is a big difference between AWS:EpochTime and time in Europe? Is that perhaps the problem?
    Here is the url of my test page: http://test.raboo.com/streamprivate/test.php

    Any suggestions?
    Thanks beforehand.
    John

  13. John says:

    It’s OK, I found another solution to fix this. Thanks in any case.

  14. Sergey says:

    Thank you very much for sharing your solution – it works great!

  15. ExtremeBt says:

    Hi,

    Thanks for the example, but:

    I’ve tested the php code and video streaming can work with wrong parameters (keypairid, privatekeyfile). Any idea? I’m setting the timestamp +10 seconds for testing. The permission in S3 where CloudFront streams has to be: Authanticated Users?

    Please help!
    Thanks in advice

  16. admin says:

    @ExtremeBt : When you can access the files with wrong parameters then there is something wrong with the authentication rules in Cloudfront.
    You must set the ACL in S3 so that only Cloudfront may read your S3 files and Cloudfront must be private.

  17. I want to express some appreciation to this writer just for bailing me out of this particular instance. Just after surfing throughout the the web and seeing basics which are not productive, I was thinking my entire life was over. Existing without the approaches to the difficulties you have sorted out all through your write-up is a critical case, and the kind that might have negatively damaged my entire career if I hadn’t encountered your web page. Your actual expertise and kindness in controlling the whole thing was very useful. I’m not sure what I would have done if I hadn’t encountered such a stuff like this. I’m able to now look ahead to my future. Thank you so much for this reliable and effective guide. I will not hesitate to endorse your web page to any individual who desires guidelines about this topic.

Leave a Reply