Flask extension that ties boto3 connectors to the application context. To be used with Python 3.7+.
-
Via
pip
:$ pip install flask-botox
-
Locally with Poetry for development purposes:
$ git clone https://github.com/jperras/flask-botox.git $ cd flask-botox $ poetry install
The main class flask_botox.Boto3
takes a Flask application as its contructor's parameter:
from flask import Flask
from flask_botox import Boto3
app = Flask(__name__)
app.config["BOTOX_SERVICES"] = ["s3", "ses", "sqs"]
botox = Boto3(app)
The application factory pattern for extensions is also valid:
from flask import Flask
from flask_botox import Boto3
botox = Boto3()
app = Flask(__name__)
app.config["BOTOX_SERVICES"] = ["s3", "ses", "sqs"]
botox.init_app(app)
Then boto3
's clients and resources will be available as properties within the application context:
>>> with app.app_context():
print(botox.clients)
print(botox.resources)
{'s3': <botocore.client.S3 object at 0x..>}
{'s3': s3.ServiceResource()}
Flask-botox uses several keys from a Flask configuration objects to customize its behaviour. Any of the AWS_*
keys are not required; if they are not specified, then the usual boto3
configuration parameter rules will apply.
AWS_ACCESS_KEY_ID
&AWS_SECRET_ACCESS_KEY
: The AWS credentials. Note that it's not a good idea to put your secret access key in a configuration file, but it can be useful for e.g. testing purposes.AWS_DEFAULT_REGION
: The region, e.g.us-east-1
, for theboto3
AWS services.AWS_PROFILE
: The AWS nanmed profile to use, if one is desired.BOTOX_SERVICES
: The name of the AWS resources you want to use, e.g.['sqs', 's3', 'ses']
.BOTOX_OPTIONAL_PARAMS
: Useful if you need to pass additional parameters to the client/resource connections, e.g. a customendpoint_url
for a particular service. The format is adict
where the top-level keys are the name of the services you're using and for each the value is adict
containing to keysargs
(contains the parameters astuple
) andkwargs
(contains the parameters as adict
when they should be passed as keyword arguments).