Created
March 6, 2020 00:18
-
-
Save njofce/d8539e5b012dc58bb76b3939fa1b933d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
KEY_ID = '*****************' | |
FILE_URL = 'http://**********.cloudfront.net/s3.png' | |
expire_date = datetime.datetime(2020, 3, 6, 11, 0, 0) | |
def rsa_signer(message): | |
with open('pk.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()) | |
def main(): | |
cloudfront_signer = CloudFrontSigner(KEY_ID, rsa_signer) | |
signed_url = cloudfront_signer.generate_presigned_url( | |
FILE_URL, date_less_than=expire_date) | |
print(signed_url) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment