简体   繁体   English

Boto3 - 如何保持 session 活着

[英]Boto3 - How to keep session alive

I have a process that is supposed to run forever and needs to updates data on a S3 bucket on AWS.我有一个应该永远运行的进程,需要更新 AWS 上 S3 存储桶上的数据。 I am initializing the session using boto3:我正在使用 boto3 初始化 session:

        session = boto3.session.Session()
        my_s3 = session.resource(
            "s3",
            region_name=my_region_name,
            aws_access_key_id=my_aws_access_key_id,
            aws_secret_access_key=my_aws_secret_access_key,
            aws_session_token=my_aws_session_token,
        )

Since the process is supposed to run for days, I am wondering how I can make sure that the session is kept alive and working.由于该过程应该运行数天,我想知道如何确保 session 保持活跃和工作。 Do I need to re-initialize the session sometimes?有时我需要重新初始化 session 吗?

Note: not sure if it is useful, but I have actually multiple threads each using its own session.注意:不确定它是否有用,但我实际上有多个线程,每个线程都使用自己的 session。

Thanks!谢谢!

There is no concept of a 'session'.没有“会话”的概念。 The Session() is simply an in-memory object that contains information about how to connect to AWS. Session()只是一个内存中的 object,其中包含有关如何连接到 AWS 的信息。

It does not actually involve any calls to AWS until an action is performed (eg ListBuckets ).在执行操作(例如ListBuckets )之前,它实际上并不涉及对 AWS 的任何调用。 Actions are RESTful, and return results immediately.操作是 RESTful 的,并立即返回结果。 They do not keep open a connection.他们不保持打开连接。

A Session is not normally required.通常不需要Session If you have stored your AWS credentials in a config file using the AWS CLI aws configure command, you can simply use:如果您已使用 AWS CLI aws configure命令将 AWS 凭证存储在配置文件中,则可以简单地使用:

import boto3

s3_resource = boto3.resource('s3')

The Session , however, is useful if you are using temporary credentials returned by an AssumeRole() command, rather than permanent credentials.但是,如果您使用Session AssumeRole()命令返回的临时凭证而不是永久凭证,则 Session 很有用。 In such a case, please note that credentials returned by AWS Security Token Service (STS) such as AssumeRole() have time limitations.在这种情况下,请注意 AWS Security Token Service (STS) 返回的凭证(例如AssumeRole()有时间限制。 This, however, is unrelated to the concept of a boto3 Session .但是,这与 boto3 Session的概念无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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