How to choose an AWS profile when using boto3 to connect to CloudFront

后端 未结 4 636
醉话见心
醉话见心 2020-11-30 19:19

I am using the Boto 3 python library, and want to connect to AWS CloudFront. I need to specify the correct AWS Profile (AWS Credentials), but looking at the official documen

相关标签:
4条回答
  • 2020-11-30 19:41

    I think the docs aren't wonderful at exposing how to do this. It has been a supported feature for some time, however, and there are some details in this pull request.

    So there are three different ways to do this:

    Option A) Create a new session with the profile

        dev = boto3.session.Session(profile_name='dev')
    

    Option B) Change the profile of the default session in code

        boto3.setup_default_session(profile_name='dev')
    

    Option C) Change the profile of the default session with an environment variable

        $ AWS_PROFILE=dev ipython
        >>> import boto3
        >>> s3dev = boto3.resource('s3')
    
    0 讨论(0)
  • 2020-11-30 19:42

    Do this to use a profile with name 'dev':

    session = boto3.session.Session(profile_name='dev')
    s3 = session.resource('s3')
    for bucket in s3.buckets.all():
        print(bucket.name)
    
    0 讨论(0)
  • 2020-11-30 19:55

    Just add profile to session configuration before client call. boto3.session.Session(profile_name='YOUR_PROFILE_NAME').client('cloudwatch')

    0 讨论(0)
  • 2020-11-30 19:57

    This section of the boto3 documentation is helpful.

    Here's what worked for me:

    session = boto3.Session(profile_name='dev')
    client = session.client('cloudfront')
    
    0 讨论(0)
提交回复
热议问题