I am trying to access csv files in S3 buckets of different profiles which are saved in aws credentials file. How can I write a script in AWS SDK in python to access different profiles at a time.
Something like this
import boto3
profile1 = boto3.<function>("profile_name1")
profile2 = boto3.<function>("profile_name2")
bucket1 = profile1.resource("S3")
bucket2 = profile2.resource("S3")
CodePudding user response:
You can do this in 3 ways as shown below -
Method 1: Create a new session with the profile
dev = boto3.session.Session(profile_name='dev')
Method 2: Change the profile of the default session in code
boto3.setup_default_session(profile_name='dev')
Method 3: Change the profile of the default session with an environment variable
$ AWS_PROFILE=dev
>>> import boto3
>>> s3dev = boto3.resource('s3')
CodePudding user response:
You can create sessions for different profiles in the following way,
import boto3
session1 = boto3.Session(profile_name='profile_name1')
profile1_s3_client = session1.client('s3') // use this to access S3 for profile_name1
session2 = boto3.Session(profile_name='profile_name2')
profile2_s3_client = session2.client('s3') // use this to access S3 for profile_name2
CodePudding user response:
You can define sessions with different profiles like mentioned below, wherein for each profile you will need access_key and secret_key. session_token is not required in the case of IAM User programmatic access
profile1 = boto3.session.Session(profile_name='dev', aws_access_key_id=access_key, aws_secret_access_key=secret_key, aws_session_token=session_token)