-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`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.
- Loading branch information
1 parent
6ee0b4d
commit 7ad45ab
Showing
20 changed files
with
729 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# S3 Example | ||
|
||
Flask-Admin example for an S3 bucket. | ||
|
||
To run this example: | ||
|
||
1. Clone the repository and navigate to this example:: | ||
|
||
git clone https://github.com/pallets-eco/flask-admin.git | ||
cd flask-admin/examples/s3 | ||
|
||
2. Create and activate a virtual environment:: | ||
|
||
python -m venv venv | ||
source venv/bin/activate | ||
|
||
3. Install requirements:: | ||
|
||
pip install -r requirements.txt | ||
|
||
4. Run the application:: | ||
|
||
python app.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from flask import Flask | ||
from flask_admin import Admin | ||
|
||
app = Flask(__name__) | ||
admin = Admin(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import os | ||
from io import BytesIO | ||
|
||
import boto3 | ||
from flask import Flask | ||
from flask_admin import Admin | ||
from flask_admin.contrib.fileadmin.s3 import S3FileAdmin | ||
from testcontainers.localstack import LocalStackContainer | ||
|
||
app = Flask(__name__) | ||
app.config["SECRET_KEY"] = "secret" | ||
admin = Admin(app) | ||
|
||
if __name__ == "__main__": | ||
with LocalStackContainer(image="localstack/localstack:latest") as localstack: | ||
s3_endpoint = localstack.get_url() | ||
os.environ["AWS_ENDPOINT_OVERRIDE"] = s3_endpoint | ||
|
||
# Create S3 client | ||
s3_client = boto3.client( | ||
"s3", | ||
aws_access_key_id="test", | ||
aws_secret_access_key="test", | ||
endpoint_url=s3_endpoint, | ||
) | ||
|
||
# Create S3 bucket | ||
bucket_name = "bucket" | ||
s3_client.create_bucket(Bucket=bucket_name) | ||
|
||
s3_client.upload_fileobj(BytesIO(b""), "bucket", "some-directory/") | ||
|
||
s3_client.upload_fileobj( | ||
BytesIO(b"abcdef"), | ||
"bucket", | ||
"some-file", | ||
ExtraArgs={"ContentType": "text/plain"}, | ||
) | ||
|
||
s3_client.upload_fileobj( | ||
BytesIO(b"abcdef"), | ||
"bucket", | ||
"some-directory/some-file", | ||
ExtraArgs={"ContentType": "text/plain"}, | ||
) | ||
|
||
# Add S3FileAdmin view | ||
admin.add_view( | ||
S3FileAdmin( | ||
bucket_name=bucket_name, | ||
s3_client=s3_client, | ||
) | ||
) | ||
|
||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
../..[s3] | ||
testcontainers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.