简体   繁体   English

使用 CookPete/react-player 从 S3 播放 HLS 视频

[英]Play HLS video from S3 using CookPete/react-player

In my s3 bucket I have two objects:在我的 s3 存储桶中,我有两个对象:

Because I'm using Storage.get from AWS Amplify, I'm able to get a signed url.因为我使用的是 AWS Amplify 的 Storage.get,所以我能够获得一个签名的 url。 So when I'm requesting the signed .m3u8 link in my front-end using the react-player https://github.com/CookPete/react-player , everything works fine.因此,当我使用 react-player https://github.com/CookPete/react-player在我的前端请求签名的 .m3u8 链接时,一切正常。 However, the player executes automatically a get request to the .ts url so I've got a 403 forbidden error because this URL is not signed.但是,播放器会自动执行对 .ts url 的 get 请求,因此我收到了 403 forbidden 错误,因为此 URL 未签名。

Before the player executes automatically the get request to the .ts file, I would like to be able to sign it using Storage.get (or to be able to do any other logic before the get request), from AWS Amplify then only after, to execute the get request with this signed .ts url.在播放器自动执行对 .ts 文件的 get 请求之前,我希望能够使用 Storage.get 对其进行签名(或者能够在 get 请求之前执行任何其他逻辑),然后从 AWS Amplify 之后,使用这个签名的 .ts url 执行 get 请求。

AWS has the amplify-video samples on GitHub here: https://github.com/awslabs/amplify-video AWS 在 GitHub 上提供了 amplify-video 示例: https : //github.com/awslabs/amplify-video

There's also a cloudformation template and details about setting up a backend to automatically create VOD assets based on a file uploaded to S3: https://aws.amazon.com/solutions/implementations/video-on-demand-on-aws/还有一个 cloudformation 模板和有关设置后端以根据上传到 S3 的文件自动创建 VOD 资产的详细信息: https : //aws.amazon.com/solutions/implementations/video-on-demand-on-aws/

This worked and simplified the setup.这有效并简化了设置。

If you need to do this via signed url, you can use AWS.CloudFront.Signer (In nodejs, must be also available in other languages):如果您需要通过签名 url 执行此操作,您可以使用 AWS.CloudFront.Signer(在 nodejs 中,必须也支持其他语言):

  const cloudFront = new AWS.CloudFront.Signer(publicKey, privateKey);
  const policy = JSON.stringify({
    Statement: [
      {
        Resource: 'https://*',
        Condition: {
          DateLessThan: {
            'AWS:EpochTime': 1757120800,
          },
        },
      },
    ],
  });
  const url = cloudFront.getSignedCookie({
    policy,
  });
  console.log(url);

Note that the public-private keys should be:请注意,公私钥应该是:

  • an SSH-2 RSA key pair.一个 SSH-2 RSA 密钥对。
  • in base64-encoded PEM format.以 base64 编码的 PEM 格式。
  • a 2048-bit key pair.一个 2048 位的密钥对。

Reference: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html#private-content-creating-cloudfront-key-pairs参考: https : //docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html#private-content-creating-cloudfront-key-pairs

The privateKey in the above will be as-it-is-generated;上面的 privateKey 将按原样生成; you might as well read the string from the private key .pem file that got generated.您不妨从生成的私钥 .pem 文件中读取字符串。 The public key, however, will be the Public Key Id, not the public key itself.但是,公钥将是公钥 ID,而不是公钥本身。 In the above reference, it tell how you need to create a key-group and upload the generate public key there.在上面的参考中,它告诉您如何创建密钥组并将生成的公钥上传到那里。 On uploading the public key, there is an id that gets assigned to it.在上传公钥时,会为其分配一个 id。 This very id needs to be passed to the first parameter of the AWS.CloudFront.Signer constructor.这个 id 需要传递给 AWS.CloudFront.Signer 构造函数的第一个参数。

However, as a side-note, if you are planning to stream a video, using a signed url approach is not recommended.但是,作为旁注,如果您打算流式传输视频,则不建议使用签名 url 方法。

The reason is simple: You are streaming because probably you want to load the videos faster and have a better video experience.原因很简单:您正在流式传输,因为您可能希望更快地加载视频并获得更好的视频体验。 Signing is CPU-expensive, so it takes some time to sign a string(here, url)(Read more here how things work under the hood: https://en.wikipedia.org/wiki/RSA_(cryptosystem)#Encryption ).It would have been fine if this was a one-time affair.签名是 CPU 昂贵的,所以需要一些时间来签署一个字符串(这里,url)(在此处阅读更多内容: https : //en.wikipedia.org/wiki/RSA_(cryptosystem ) #Encryption ) .如果这是一次性的事情就好了。 But the signing needs to be done for every .ts url also.但是也需要为每个 .ts url 进行签名。

I have explained how to do this here: https://stackoverflow.com/a/67929204/5657783我在这里解释了如何做到这一点: https : //stackoverflow.com/a/67929204/5657783

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM