【问题标题】:Cloudfront URL never expiresCloudfront URL 永不过期
【发布时间】:2019-08-08 16:18:09
【问题描述】:

场景: 我正在尝试为 s3 中的对象生成云端签名的 url。

步骤:
1. 在 s3 存储桶中创建一个对象并将其公开。
2. 创建了指向该 s3 存储桶的云端分发。
3. 使用下面的代码生成签名的url

以下是从他们的docs 生成云端签名网址的代码。

import datetime

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from botocore.signers import CloudFrontSigner


def rsa_signer(message):
    with open('path/to/key.pem', 'rb') as key_file:
        private_key = serialization.load_pem_private_key(
        key_file.read(),
        password=None,
        backend=default_backend()
       )
    return private_key.sign(message, padding.PKCS1v15(), hashes.SHA1())

key_id = 'AKIAIOSFODNN7EXAMPLE'
url = 'https://d2949o5mkkp72v.cloudfront.net/hello.txt'
expire_date = datetime.datetime(2017, 1, 1)

cloudfront_signer = CloudFrontSigner(key_id, rsa_signer)

# Create a signed url that will be valid until the specfic expiry date
# provided using a canned policy.
signed_url = cloudfront_signer.generate_presigned_url(url, date_less_than=expire_date)
print(signed_url)

输出:

https://d2949o5mkkp72v.cloudfront.net/hello.txt?Expires=1483228800&Signature=some_signature&Key-Pair-Id=AKIAIOSFODNN7EXAMPLE

上面的 url 指向一个日期,但我仍然可以通过这个 URL 访问该对象。我还可以通过截断 Signature 和 Key-Pair-Id 查询参数来访问该对象。

这里可能出了什么问题?

【问题讨论】:

    标签: python amazon-web-services boto3 amazon-cloudfront pre-signed-url


    【解决方案1】:

    我找到了问题的解决方案。错误实际上不在代码中,而是在云端分发的配置中。

    缺少以下配置:

    希望对你有帮助:)

    如果编辑现有分发的配置,您会在“行为”选项卡的行为记录的编辑设置中找到此“限制查看者访问”设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多