AWS CF Signer - 简化 CloudFront 和 Signed Cookies 的签名过程
AWS CF Signer 是一个简单的 Python 库,可以帮助您轻松地为 Amazon CloudFront 分发和自定义-signed cookies 创建签名。它消除了手动创建所需哈希和日期的过程,为您提供了一个易于使用的接口。
项目简介
AWS CF Signer 可以让您:
- 自动为 CloudFront 分发创建签名 URL。
- 使用自定义策略生成 Signed Cookies。
- 支持多种算法(包括 HMAC-SHA1、HMAC-SHA256 和 RSA)。
使用此库可以节省时间,减少出错的可能性,并简化与 Amazon CloudFront 相关的签名过程。
如何使用
首先,安装 AWS CF Signer:
pip install aws-cf-signer
接下来,您可以导入并使用 aws_cf_signer
模块来创建签名 URL 或 signed cookies。
创建签名 URL
以下示例展示了如何使用 AWS CF Signer 为特定分布和路径创建签名 URL:
import json
from datetime import timedelta
from aws_cf_signer import generate_presigned_url
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
distribution_id = 'YOUR_DISTRIBUTION_ID'
url_path = '/path/to/your/resource'
expiration = (datetime.utcnow() + timedelta(hours=1)).isoformat()
policy_document = json.dumps({
"Statement": [
{
"Resource": f"https://{distribution_id}.cloudfront.net{url_path}",
"Condition": {
"DateLessThan": {"AWS:EpochTime": expiration}
}
}
]
})
signed_url = generate_presigned_url(
access_key, secret_key,
distribution_id, url_path, policy_document=policy_document,
algorithm='HMAC-SHA256')
print(signed_url)
生成Signed Cookies
以下示例演示了如何使用 AWS CF Signer 为特定分布和策略创建 signed cookie:
import json
from aws_cf_signer import generate_signed_cookies
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
distribution_id = 'YOUR_DISTRIBUTION_ID'
cookie_expiration = (datetime.utcnow() + timedelta(hours=1)).isoformat()
policy_document = json.dumps({
"Statement": [
{
"Resource": f"https://{distribution_id}.cloudfront.net/*",
"Condition": {
"DateLessThan": {"AWS:EpochTime": cookie_expiration}
}
}
]
})
cookies = generate_signed_cookies(access_key, secret_key, distribution_id,
policy_document=policy_document,
algorithm='HMAC-SHA256')
print(cookies)
特点
AWS CF Signer 具有以下特点:
- 易于使用:提供简洁的 API,便于集成到现有代码中。
- 多种算法支持:支持 HMAC-SHA1、HMAC-SHA256 和 RSA 加密算法。
- 安全性:确保通过安全的方法处理敏感数据,如访问密钥和秘密密钥。
尝试 AWS CF Signer!
如果您正在寻找一种简化 Amazon CloudFront 分发和 signed cookies 签名过程的方法,AWS CF Signer 是一个不错的选择。要开始使用,请访问以下链接:
<>
让我们知道您对 AWS CF Signer 的看法!欢迎在项目的 GitHub 页面上提交反馈或问题。
再次强调,如果感兴趣的话,请尝试 <https://gitcode