简体   繁体   中英

Python: how to generate signed URL with custom policy cloudfront

I have some code that generates a signed URL

expire_time = in_an_hour()
conn = CloudFrontConnection(access_key_id, access_key)

##enter the id or domain name to select a distribution
distribution = Distribution(connection=conn, config=None, domain_name=domain, id=dist_id, last_modified_time=None, status='')
signed_url = distribution.create_signed_url(url=url_tosign, keypair_id=keypair,expire_time=expire_time,private_key_file="/path/to/priv.pem")
return signed_url

I want to include a custom policy that takes into account user IP. I wrote a method to generate the json that works fine. I tried adding policy=get_policy(url_tosign, ip) to the distribution.create_signed_url call, but I just get an error saying it's an unexpected keyword argument. How can I modify this code to generate my signed URL with source IP restrictions?

James Dean's link had the solution. There is an ip_address parameter that we can use.

Updated line of code

signed_url = distribution.create_signed_url(url=url_tosign, keypair_id=keypair,expire_time=expire_time,private_key_file="/path/to/priv.pem", ip_address= "x.x.x.x/32") # CIDR notation optional. x.x.x.x also works as a /32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM