-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign_url.py
47 lines (34 loc) · 1.21 KB
/
sign_url.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from datetime import datetime, timedelta
from botocore import session
from botocore.signers import CloudFrontSigner
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding
REGION = 'ap-northeast-1'
PROFILE = 'default'
URL = 'https://example.com/index.html'
KEY_ID = 'xxxxxxxxxxx'
PRIVATE_KEY_PATH = './pk-xxxxxxxxxxxxxx.pem'
EXPIRE = 1
def get_rsa_signer(message):
with open(PRIVATE_KEY_PATH, 'rb') as f:
private_key = serialization.load_pem_private_key(
data=f.read(),
password=None,
backend=default_backend(),
)
return private_key.sign(message, padding.PKCS1v15(), hashes.SHA1())
def main():
sess = session.get_session()
session.profile = SYSOP
session.region = REGION
# JST, not use timezone
expire = datetime.now() + timedelta(minutes=EXPIRE) - timedelta(hours=9)
cloudfront_signer = CloudFrontSigner(KEY_ID, get_rsa_signer)
signed_url = cloudfront_signer.generate_presigned_url(
URL,
date_less_than=expire,
)
print(signed_url)
if __name__ == "__main__":
main()