Search code examples
javaamazon-web-servicesvideo-streamingjwplayeramazon-cloudfront

aws cloudfront Private streaming setup using and expirable links generation using Java


I am trying to setup secure streaming with amazon s3 and cloudfront. I have done the following steps and I always get an error saying

"stream not found".

  1. Setup s3 bucket and uploaded mp3 file(file.mp3) for testing.
  2. Created origin access policy in cloudfront, got Id and S3 canonicle Used Id.
  3. Given S3 canonicle User Id read permissions to s3 bucket. (For testing I have given read permission to everyone as well.)
  4. Created RSA public/private key for cloudfront.
  5. Created Streaming distribution in cloudfront with previous s3 bucket as origin and Trusted Signer as self. Result of this request shows self trusted signer with public key Id.
  6. Generated signed url using private key.
  7. Created html file to show this mp3 using jwplayer.

Below is the code I used to create origin signed url

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    String distributionDomain = "sbngg4fbfkiq2.cloudfront.net";
    String privateKeyFilePath = "src/s3access/pk-APKAJRHSQBGT5CW7P2CA.der";
    String s3ObjectKey = "file.mp3";
    // Convert your DER file into a byte array.
    byte[] derPrivateKey = ServiceUtils.readInputStreamToBytes(new
            FileInputStream(privateKeyFilePath));
    String signedUrlCanned = CloudFrontService.signUrlCanned(
       s3ObjectKey, // Resource URL or Path
        "APKAJRHSQBGT5CW7P2CA.",     // Certificate identifier, 
                       // an active trusted signer for the distribution
        derPrivateKey, // DER Private key data
        new Date(112,3,29) // DateLessThan
        );
    System.out.println(signedUrlCanned);

This gives signed url. Then I have used following html file.

<html><head>
<script type='text/javascript' src='jwplayer.js'></script>
</head><body>
<div id="container">Loading the player ...</div>

<script type="text/javascript">

  jwplayer('container').setup({
    'id': 'playerID',
    'width': '352',
    'height': '240',
'provider': 'rtmp',
    'streamer': 'rtmp://sbngg4fbfkiq2.cloudfront.net/cfx/st',
    'file': 
    'file.mp3?Expires=1335637800&Signature=asSUSIbGYZP9IXnmjWIKgXMP2DCCA1B-x0mQtsQbUBFWdbKzlU~NnId8VH8T5ww8I1nbZKHWoLpGr679~QoDSMScKpVxVbyYVjQ0kn-JejzSkM6~ZEC6r8nArEdrT9R-M6EjR~IBkuPD-0qW2OUk1MBC4oiNeytIolG6IrSTGOU_&Key-Pair-Id=APKAJRHSQBGT5CW7P2CA',
'modes': [
    {
        type: 'flash', src: 'player.swf'
    } 
]
 });

</script>

</body>
</html>

It displays video frame. Clicking on play button tried to load stream but after couple of seconds it says

 Stream not foundfile.mp3?Expires=1335637800&Signature=asSUSIbGYZP9IXnmjWIKgXMP2DCCA1B-x0mQtsQbUBFWdbKzlU~NnId8VH8T5ww8I1nbZKHWoLpGr679~QoDSMScKpVxVbyYVjQ0kn-JejzSkM6~ZEC6r8nArEdrT9R-M6EjR~IBkuPD-0qW2OUk1MBC4oiNeytIolG6IrSTGOU_&Key-Pair-Id=APKAJRHSQBGT5CW7P2CA

Please help me to setup this. Let me know if I am missing something.


Solution

  • I got it working with same setup I mentioned above, but few cases

    1. It is not working for mp3 files (checked with one file I have uploaded) but works for mp4 videos.
    2. Suppose I generated signed link for the file which is not present and tried to load video. Ofcourse I will get error. After this even I upload the file with same name, still I continue to get the error.