Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

amazon s3 - Python boto3 validate Session

Im running the below code whenever i need to upload files to S3:

client = boto3.session.Session().client(self.client_type, use_ssl=True)
# use the client to upload

However this mean i do a request to auth to S3 for every time i need to upload a file. In my case there are alot of files i want to share if session is still open for the same client, but i could not find a way to detect via boto3 if the session is still valid, is there a way ?

Currently im tracking 1H time in my app wise to do this:

class ArchiveStorage(object):

    client = None
    session_duration_seconds = 60 * 60  # One Hour
    client_type = "s3"
    last_session = None

    def __init__(self):
        if ArchiveStorage.client is None or not self._is_session_valid():
            ArchiveStorage.client = self._create_new_s3_session()
        self.client = ArchiveStorage.client

    def _create_new_s3_session(self):
        return boto3.session.Session().client(self.client_type, use_ssl=True)

    def _is_session_valid(self):
        if not self.last_session:
            return False
        now = get_logical_timestamp()
        if now - ArchiveStorage.last_session > self.session_duration_seconds:
            return False
        return True

Is there a way to validate a S3 session without needing to calculate the session time in app side ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...