简体   繁体   中英

boto3 s3 initialized session return credentials

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? Ie how can I extract the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the initialized boto3 s3 client?

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM