Skip to content

Commit 7ad45ab

Browse files
Use boto3 for S3 fileadmin
`boto` has long since been deprecated and replaced by `boto3`. This patch moves over to using the new `boto3` library for interacting with the S3 bucket. This is a breaking change because the interface for S3FileAdmin also changes - rather than taking the AWS keys directly, it now takes a boto.client('s3') instance.
1 parent 6ee0b4d commit 7ad45ab

File tree

20 files changed

+729
-132
lines changed

20 files changed

+729
-132
lines changed

doc/advanced.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ can use it by adding a FileAdmin view to your app::
162162

163163

164164
FileAdmin also has out-of-the-box support for managing files located on a Amazon Simple Storage Service
165-
bucket. To add it to your app::
165+
bucket using a `boto3 client <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/core/session.html#boto3.session.Session.client>`_. To add it to your app::
166166

167167
from flask_admin import Admin
168168
from flask_admin.contrib.fileadmin.s3 import S3FileAdmin
169169

170170
admin = Admin()
171171

172-
admin.add_view(S3FileAdmin('files_bucket', 'us-east-1', 'key_id', 'secret_key')
172+
admin.add_view(S3FileAdmin(boto3.client('s3'), 'files_bucket'))
173173

174174
You can disable uploads, disable file deletion, restrict file uploads to certain types, etc.
175175
Check :mod:`flask_admin.contrib.fileadmin` in the API documentation for more details.

doc/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
2.0.0a2
5+
-------
6+
7+
Breaking changes:
8+
9+
* Use of the `boto` library has been replaced by `boto3`. S3FileAdmin and S3Storage now accept a `boto3.client('s3')` instance rather than AWS access+secret keys directly.
10+
411
2.0.0a1
512
-------
613

examples/s3/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# S3 Example
2+
3+
Flask-Admin example for an S3 bucket.
4+
5+
To run this example:
6+
7+
1. Clone the repository and navigate to this example::
8+
9+
git clone https://github.com/pallets-eco/flask-admin.git
10+
cd flask-admin/examples/s3
11+
12+
2. Create and activate a virtual environment::
13+
14+
python -m venv venv
15+
source venv/bin/activate
16+
17+
3. Install requirements::
18+
19+
pip install -r requirements.txt
20+
21+
4. Run the application::
22+
23+
python app.py

examples/s3/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from flask import Flask
2+
from flask_admin import Admin
3+
4+
app = Flask(__name__)
5+
admin = Admin(app)

examples/s3/app.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import os
2+
from io import BytesIO
3+
4+
import boto3
5+
from flask import Flask
6+
from flask_admin import Admin
7+
from flask_admin.contrib.fileadmin.s3 import S3FileAdmin
8+
from testcontainers.localstack import LocalStackContainer
9+
10+
app = Flask(__name__)
11+
app.config["SECRET_KEY"] = "secret"
12+
admin = Admin(app)
13+
14+
if __name__ == "__main__":
15+
with LocalStackContainer(image="localstack/localstack:latest") as localstack:
16+
s3_endpoint = localstack.get_url()
17+
os.environ["AWS_ENDPOINT_OVERRIDE"] = s3_endpoint
18+
19+
# Create S3 client
20+
s3_client = boto3.client(
21+
"s3",
22+
aws_access_key_id="test",
23+
aws_secret_access_key="test",
24+
endpoint_url=s3_endpoint,
25+
)
26+
27+
# Create S3 bucket
28+
bucket_name = "bucket"
29+
s3_client.create_bucket(Bucket=bucket_name)
30+
31+
s3_client.upload_fileobj(BytesIO(b""), "bucket", "some-directory/")
32+
33+
s3_client.upload_fileobj(
34+
BytesIO(b"abcdef"),
35+
"bucket",
36+
"some-file",
37+
ExtraArgs={"ContentType": "text/plain"},
38+
)
39+
40+
s3_client.upload_fileobj(
41+
BytesIO(b"abcdef"),
42+
"bucket",
43+
"some-directory/some-file",
44+
ExtraArgs={"ContentType": "text/plain"},
45+
)
46+
47+
# Add S3FileAdmin view
48+
admin.add_view(
49+
S3FileAdmin(
50+
bucket_name=bucket_name,
51+
s3_client=s3_client,
52+
)
53+
)
54+
55+
app.run(debug=True)

examples/s3/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
../..[s3]
2+
testcontainers

examples/sqla/admin/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,4 @@ def get_locale():
2222
babel = Babel(app, locale_selector=get_locale)
2323

2424

25-
# Initialize babel
26-
babel = Babel(app, locale_selector=get_locale)
27-
28-
2925
import admin.main # noqa: F401, E402

0 commit comments

Comments
 (0)