Skip to content

Instantly share code, notes, and snippets.

@FanchenBao
Created December 29, 2019 02:58
Show Gist options
  • Save FanchenBao/c7d171a9d8c60a6ed8aa8738efd9f240 to your computer and use it in GitHub Desktop.
Save FanchenBao/c7d171a9d8c60a6ed8aa8738efd9f240 to your computer and use it in GitHub Desktop.
Use `requests` to obtain temporary credential for accessing AWS S3 from AWS IoT device certificate and private key
import requests
credential_provider_endpoint = 'https://<your_credentials_provider_endpoint>/role-aliases/iot-s3-access-role-alias/credentials'
device_cert_path = '<path_to_device_cert>'
device_private_key_path = '<path_to_device_private_key>'
resp = requests.get(
credential_provider_endpoint,
headers={'x-amzn-iot-thingname': 'TestThing'},
cert=(device_cert_path, device_private_key_path),
)
if resp: # check whether https request succeeds
credentials = resp.json()
access_key_id = credentials['credentials']['accessKeyId']
secrete_access_key = credentials['credentials']['secretAccessKey']
session_token = credentials['credentials']['sessionToken']
else:
print('error requesting temporary access to AWS S3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment