-
-
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.
Merge pull request #2552 from pallets-eco/boto-to-boto3
Use boto3 for S3 fileadmin
- Loading branch information
Showing
20 changed files
with
778 additions
and
130 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 |
Empty file.
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,57 @@ | ||
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 flask_babel import Babel | ||
from testcontainers.localstack import LocalStackContainer | ||
|
||
app = Flask(__name__) | ||
app.config["SECRET_KEY"] = "secret" | ||
admin = Admin(app) | ||
babel = Babel(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.