Access S3 resources via Cognito in Python

Purpose

Here's the Python code that gets temporary credentials via Cognito and uses that information to access S3. As for Python, only an incomplete implementation has been dropped, so I will post it.

Preparation

On the Cognito side, give the minimum required authority to the S3 resource in advance in the non-authentication role.

Implementation

Below is the code.

cognito-s3.py


        client = boto3.client('cognito-identity', 'ap-northeast-1')

        #Get a Cognito authentication ID on the first access
        resp =  client.get_id(IdentityPoolId='ap-northeast-1:<YOUR COGNITO IDENTITY POOL ID>')
        print "\nIdentity ID: %s"%(resp['IdentityId'])
        print "\nRequest ID: %s"%(resp['ResponseMetadata']['RequestId'])

        #Get credentials to establish a Session on the second access
        resp = client.get_credentials_for_identity(IdentityId=resp['IdentityId'])
        secretKey = resp['Credentials']['SecretKey']
        accessKey = resp['Credentials']['AccessKeyId']
        token = resp['Credentials']['SessionToken']
        print "\nToken: %s"%(token)
        print "\nSecretKey: %s"%(secretKey)
        print "\nAccessKey ID: %s"%(accessKey)
        print resp

        #Access S3 Object with credentials
        session = Session(aws_access_key_id=accessKey,
                  aws_secret_access_key=secretKey,
                  aws_session_token=token, #Don't forget your token!
                  region_name='ap-northeast-1')
        s3 = session.resource('s3')
        obj = s3.Object(bucket_name='<BUCKET NAME>', key='<KEY NAME>')

        #The following is an example of simply reading and returning the length
        response = obj.get()
        data = response['Body'].read()
        print len(data)

Recommended Posts

Access S3 resources via Cognito in Python
Try PLC register access in Python
Access the Twitter API in Python
How to access environment variables in Python
Revived from "no internet access" in Python
PUT gzip directly to S3 in Python
Exclusive file access between processes in Python
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Get the weather in Osaka via WebAPI (python)
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Get data from database via ODBC with Python (Access)
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Edit fonts in Python
Singleton pattern in Python
Read DXF in python
Daily AtCoder # 53 in Python
Use config.ini in Python
Daily AtCoder # 33 in Python