开始使用 Python 进行安全 AWS CloudFront 流式传输
我已经创建了一个 S3 存储桶,上传了一个视频,并在 CloudFront 中创建了一个流分配。用静态 HTML 播放器对其进行了测试,它可以工作。我已经通过帐户设置创建了密钥对。目前我的桌面上有私钥文件。这就是我所在的地方。
我的目标是让我的 Django/Python 网站创建安全 URL,并且人们无法访问视频,除非它们来自我的页面之一。问题是我对亚马逊的布局方式过敏,而且我越来越困惑。
我意识到这不会是 StackOverflow 上最好的问题,但我确信我不会是这里唯一一个无法弄清楚如何设置安全的 CloudFront/S3 情况的傻瓜。我非常感谢您的帮助,并且愿意(两天后)对最佳答案给予 500pt 的奖励。
我有几个问题,一旦得到解答,应该适合如何完成我所追求的任务的一个解释:
在文档中(下一点中有一个示例)有很多 XML 告诉我我需要
POST
东西到不同的地方。有在线控制台可以执行此操作吗?或者我确实必须通过 cURL(等)强制执行此操作?如何为 CloudFront 创建源访问身份并将其绑定到我的分配?我已阅读本文档,但是,根据第一点,不要不知道该怎么办。我的密钥对如何适应这种情况?
完成后,如何限制 S3 存储桶仅允许人们通过该身份下载内容?如果这是另一个 XML 工作,而不是点击 Web UI,请告诉我应该在哪里以及如何将其添加到我的帐户中。
在 Python 中,为文件生成过期 URL 的最简单方法是什么。我安装了
boto
,但我不知道如何从流分发中获取文件。是否有任何应用程序或脚本可以克服设置此服装的困难?我使用 Ubuntu (Linux),但如果虚拟机仅支持 Windows,则我在虚拟机中安装了 XP。我已经研究过 CloudBerry S3 Explorer Pro - 但它与在线 UI 一样有意义。
I have created a S3 bucket, uploaded a video, created a streaming distribution in CloudFront. Tested it with a static HTML player and it works. I have created a keypair through the account settings. I have the private key file sitting on my desktop at the moment. That's where I am.
My aim is to get to a point where my Django/Python site creates secure URLs and people can't access the videos unless they've come from one of my pages. The problem is I'm allergic to the way Amazon have laid things out and I'm just getting more and more confused.
I realise this isn't going to be the best question on StackOverflow but I'm certain I can't be the only fool out here that can't make heads or tails out of how to set up a secure CloudFront/S3 situation. I would really appreciate your help and am willing (once two days has passed) give a 500pt bounty to the best answer.
I have several questions that, once answered, should fit into one explanation of how to accomplish what I'm after:
In the documentation (there's an example in the next point) there's lots of XML lying around telling me I need to
POST
things to various places. Is there an online console for doing this? Or do I literally have to force this up via cURL (et al)?How do I create a Origin Access Identity for CloudFront and bind it to my distribution? I've read this document but, per the first point, don't know what to do with it. How does my keypair fit into this?
Once that's done, how do I limit the S3 bucket to only allow people to download things through that identity? If this is another XML jobby rather than clicking around the web UI, please tell me where and how I'm supposed to get this into my account.
In Python, what's the easiest way of generating an expiring URL for a file. I have
boto
installed but I don't see how to get a file from a streaming distribution.Are there are any applications or scripts that can take the difficulty of setting this garb up? I use Ubuntu (Linux) but I have XP in a VM if it's Windows-only. I've already looked at CloudBerry S3 Explorer Pro - but it makes about as much sense as the online UI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,需要大量的 API 工作才能完成此设置。我希望他们很快就能在 AWS 控制台中获得它!
更新:我已将此代码提交给 boto - 从 boto v2.1(2011 年 10 月 27 日发布)开始,这变得更加容易。对于 boto < 2.1、使用此处的说明。对于 boto 2.1 或更高版本,请在我的博客上获取更新的说明:http ://www.secretmike.com/2011/10/aws-cloudfront-secure-streaming.html 一旦 boto v2.1 被更多发行版打包,我将更新答案 要实现您想要的目标,
您需要执行以下步骤,我将在下面详细介绍这些步骤:
1 - 创建存储桶并上传对象
最简单的方法是通过 AWS 控制台,但为了完整起见,我将展示如何使用 boto。 Boto 代码如下所示:
2 - 创建 Cloudfront“源访问身份”
目前,此步骤只能使用 API 执行。 Boto 代码位于:
3 - 修改对象上的 ACL
现在我们已经有了特殊的 S3 用户帐户(我们上面创建的 S3CanonicalUserId),我们需要授予它对 s3 对象的访问权限。我们可以使用 AWS 控制台轻松完成此操作,方法是打开对象的(不是存储桶的!)权限选项卡,单击“添加更多权限”按钮,然后将上面获得的很长的 S3CanonicalUserId 粘贴到新权限的“被授权者”字段中。确保您授予新权限“打开/下载”权限。
您还可以使用以下 boto 脚本在代码中执行此操作:
4 - 创建 Cloudfront 发行版
请注意,在 Boto 2.0 版本(当时尚未正式发布)之前,boto 并不完全支持自定义来源和私有发行版写作。下面的代码从 boto 2.0 分支中提取一些代码并将其组合在一起以使其运行,但它并不漂亮。 2.0 分支处理这个问题更加优雅 - 如果可能的话,绝对使用它!
5 - 测试您可以从 cloudfront 下载对象,但不能从 s3 下载对象
您现在应该能够验证:
必须调整测试以适应您的情况流播放器,但基本思想是只有基本的 cloudfront url 才可以工作。
6 - 为 CloudFront 创建密钥对
我认为执行此操作的唯一方法是通过 Amazon 的网站。进入您的 AWS“帐户”页面并单击“安全凭证”链接。单击“密钥对”选项卡,然后单击“创建新密钥对”。这将为您生成一个新的密钥对并自动下载私钥文件(pk-xxxxxxxxx.pem)。确保密钥文件安全且私密。另请记下亚马逊的“密钥对 ID”,因为我们在下一步中将需要它。
7 - 在 Python 中生成一些 URL
从 boto 版本 2.0 开始,似乎不支持生成签名的 CloudFront URL。 Python 的标准库中不包含 RSA 加密例程,因此我们必须使用额外的库。我在这个例子中使用了 M2Crypto。
对于非流式传输分发,您必须使用完整的 cloudfront URL 作为资源,但是对于流式传输,我们仅使用视频文件的对象名称。请参阅下面的代码,了解生成仅持续 5 分钟的 URL 的完整示例。
此代码大致基于 Amazon 在 CloudFront 文档中提供的 PHP 示例代码。
8 - 尝试 URL
希望您现在应该有一个如下所示的工作 URL:
将其放入您的 js 中,您应该拥有如下所示的内容(来自 Amazon CloudFront 文档中的 PHP 示例) ):
总结
如您所见,这并不容易! boto v2 对设置发行版有很大帮助。我会看看是否可以在那里获取一些 URL 生成代码来改进这个伟大的库!
You're right, it takes a lot of API work to get this set up. I hope they get it in the AWS Console soon!
UPDATE: I have submitted this code to boto - as of boto v2.1 (released 2011-10-27) this gets much easier. For boto < 2.1, use the instructions here. For boto 2.1 or greater, get the updated instructions on my blog: http://www.secretmike.com/2011/10/aws-cloudfront-secure-streaming.html Once boto v2.1 gets packaged by more distros I'll update the answer here.
To accomplish what you want you need to perform the following steps which I will detail below:
1 - Create Bucket and upload object
The easiest way to do this is through the AWS Console but for completeness I'll show how using boto. Boto code is shown here:
2 - Create a Cloudfront "Origin Access Identity"
For now, this step can only be performed using the API. Boto code is here:
3 - Modify the ACLs on your objects
Now that we've got our special S3 user account (the S3CanonicalUserId we created above) we need to give it access to our s3 objects. We can do this easily using the AWS Console by opening the object's (not the bucket's!) Permissions tab, click the "Add more permissions" button, and pasting the very long S3CanonicalUserId we got above into the "Grantee" field of a new. Make sure you give the new permission "Open/Download" rights.
You can also do this in code using the following boto script:
4 - Create a cloudfront distribution
Note that custom origins and private distributions are not fully supported in boto until version 2.0 which has not been formally released at time of writing. The code below pulls out some code from the boto 2.0 branch and hacks it together to get it going but it's not pretty. The 2.0 branch handles this much more elegantly - definitely use that if possible!
5 - Test that you can download objects from cloudfront but not from s3
You should now be able to verify:
The tests will have to be adjusted to work with your stream player, but the basic idea is that only the basic cloudfront url should work.
6 - Create a keypair for CloudFront
I think the only way to do this is through Amazon's web site. Go into your AWS "Account" page and click on the "Security Credentials" link. Click on the "Key Pairs" tab then click "Create a New Key Pair". This will generate a new key pair for you and automatically download a private key file (pk-xxxxxxxxx.pem). Keep the key file safe and private. Also note down the "Key Pair ID" from amazon as we will need it in the next step.
7 - Generate some URLs in Python
As of boto version 2.0 there does not seem to be any support for generating signed CloudFront URLs. Python does not include RSA encryption routines in the standard library so we will have to use an additional library. I've used M2Crypto in this example.
For a non-streaming distribution, you must use the full cloudfront URL as the resource, however for streaming we only use the object name of the video file. See the code below for a full example of generating a URL which only lasts for 5 minutes.
This code is based loosely on the PHP example code provided by Amazon in the CloudFront documentation.
8 - Try out the URLs
Hopefully you should now have a working URL which looks something like this:
Put this into your js and you should have something which looks like this (from the PHP example in Amazon's CloudFront documentation):
Summary
As you can see, not very easy! boto v2 will help a lot setting up the distribution. I will find out if it's possible to get some URL generation code in there as well to improve this great library!
您可以为资源生成过期的签名 URL。 Boto3 文档有一个很好的示例解决方案:
You can generate a expiring signed-URL for the resource. Boto3 documentation has a nice example solution for that: