Home > Blockchain >  boto3 s3 initialized session return credentials
boto3 s3 initialized session return credentials

Time:05-16

I am facing something similar to How to load file from custom hosted Minio s3 bucket into pandas using s3 URL format?

however, I already have an initialized s3 session (from boto3). How can I get the credentials returned from it to feed these directly to pandas? I.e. how can I extract the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the initialized boto3 s3 client?

CodePudding user response:

You can use session.get_credentials

import boto3

session = boto3.Session()
credentials = session.get_credentials()

AWS_ACCESS_KEY_ID = credentials.access_key
AWS_SECRET_ACCESS_KEY = credentials.secret_key
AWS_SESSION_TOKEN = credentials.token

If you only have access to boto client (like the S3 client), you can find the credentials hidden here:

client = boto3.client("s3")

client._request_signer._credentials.access_key
client._request_signer._credentials.secret_key
client._request_signer._credentials.token

If you don't want to handle credentials (I assume you're using the SSO here), you can load the S3 object directly with pandas: pd.read_csv(s3_client.get_object(Bucket='Bucket', Key ='FileName').get('Body'))

  • Related