在哪里可以找到使用boto3编写自定义AWS凭证提供程序的文档? [英] Where can I find the documentation for writing custom AWS credential provider using boto3?

查看:93
本文介绍了在哪里可以找到使用boto3编写自定义AWS凭证提供程序的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个python进程来在运行时刷新临时AWS凭证(有效期为30分钟),以确保我的代码可以连续运行30分钟以上.

I am looking to create a python process to refresh temporary AWS credentials (valid for 30 mins) at runtime to ensure my code can run continuously for more than 30 mins.

什么是RefreshableCredentials?如何使用?

What is RefreshableCredentials and how do I use it?

推荐答案

经过一番摸索,我终于得出结论,没有记录botocore和boto3类.

After a lot of poking around, I finally came to conclusion that the botocore and boto3 classes are not documented.

我查看了源代码,并实现了适用于我的用例的解决方案.将其发布在这里供其他人参考.

I looked at the source code and have implemented a solution that works for my use case. Posting it here for others to refer.

class AWSCredsRefresh:

    def run(self):
        session = get_session()
        cred_provider = session.get_component('credential_provider')

        cred_provider.insert_before('env', CustomCredentialProvider())

        boto3_session = Session(botocore_session=session)
        #Perform AWS operations with boto3_session

class CustomCredentialProvider(CredentialProvider):
    CANONICAL_NAME = "custom-creds"

    def __init__(self):


    def load(self):
        #These creds will be automatically refreshed using the _refreh method if the current creds are going to expire in 15 mins or less
        creds = DeferredRefreshableCredentials(refresh_using=self._refresh, method="sts-assume-role",)
        return creds

    def _refresh(self):
        #Refresh your AWS creds using custom process
        response = self._custom_aws_cred_refresh()
        credentials = {
            "access_key": response.get("AccessKeyId"),
            "secret_key": response.get("SecretAccessKey"),
            "token": response.get("SessionToken"),
            "expiry_time": response.get("Expiration").isoformat(),
        }
        return credentials

    def _custom_aws_cred_refresh(self):
        #Your custom AWS cred refresh code
        return response


if __name__ == '__main__':
    obj = AWSCredsRefresh()
    obj.run()

这篇关于在哪里可以找到使用boto3编写自定义AWS凭证提供程序的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆