Boto3: get credentials dynamically?

后端 未结 3 1246
暗喜
暗喜 2020-12-08 09:58

I am struggling to find out how I can get my aws_access_key_id and aws_secret_access_key dynamically from my code.

In boto2 I could do the following: boto.con

3条回答
  •  攒了一身酷
    2020-12-08 10:21

    It's generally a best practice to only use temporary credentials. You can get temporary credentials with STS.get_session_token.

    EDIT: As of this PR, you can access the current session credentials like so:

    import boto3
    
    session = boto3.Session()
    credentials = session.get_credentials()
    
    # Credentials are refreshable, so accessing your access key / secret key
    # separately can lead to a race condition. Use this to get an actual matched
    # set.
    credentials = credentials.get_frozen_credentials()
    access_key = credentials.access_key
    secret_key = credentials.secret_key
    
    redshift = session.client('redshift')
    ...
    

    I would still recommend using temporary credentials scoped to exactly what redshift needs.

提交回复
热议问题