在哪里可以找到使用 boto3 编写自定义 AWS 凭证提供程序的文档?

Pra*_*lgi 5 python amazon-s3 amazon-web-services boto3

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

什么是 RefreshableCredentials 以及如何使用它?

Pra*_*lgi 5

经过大量研究后,我终于得出结论,botocore 和 boto3 类没有文档记录。

我查看了源代码并实现了适合我的用例的解决方案。贴在这里供其他人参考。

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()
Run Code Online (Sandbox Code Playgroud)