-
Notifications
You must be signed in to change notification settings - Fork 11
Add support for S3 regions #962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Generated by Django 4.2.10 on 2025-12-05 20:03 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("main", "0078_classification_applied_to"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="s3storagesource", | ||
| name="region", | ||
| field=models.CharField(blank=True, max_length=255, null=True), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ | |||
| from dataclasses import dataclass | ||||
|
|
||||
| import boto3 | ||||
| import boto3.resources.base | ||||
| import boto3.session | ||||
| import botocore | ||||
| import botocore.config | ||||
| import botocore.exceptions | ||||
|
|
@@ -37,6 +37,7 @@ class S3Config: | |||
| secret_access_key: str | ||||
| bucket_name: str | ||||
| prefix: str | ||||
| region: str | None = None | ||||
| public_base_url: str | None = None | ||||
|
|
||||
| sensitive_fields = ["access_key_id", "secret_access_key"] | ||||
|
|
@@ -94,26 +95,36 @@ def get_session(config: S3Config) -> boto3.session.Session: | |||
| session = boto3.Session( | ||||
| aws_access_key_id=config.access_key_id, | ||||
| aws_secret_access_key=config.secret_access_key, | ||||
| region_name=config.region, | ||||
| ) | ||||
| return session | ||||
|
|
||||
|
|
||||
| def get_s3_client(config: S3Config) -> S3Client: | ||||
| session = get_session(config) | ||||
|
|
||||
| # Always use signature version 4 | ||||
| boto_config = botocore.config.Config(signature_version="s3v4") | ||||
|
|
||||
| if config.endpoint_url: | ||||
| client = session.client( | ||||
| service_name="s3", | ||||
| endpoint_url=config.endpoint_url, | ||||
| aws_access_key_id=config.access_key_id, | ||||
| aws_secret_access_key=config.secret_access_key, | ||||
| config=botocore.config.Config(signature_version="s3v4"), | ||||
| region_name=config.region, | ||||
| config=boto_config, | ||||
| ) | ||||
| else: | ||||
| client = session.client( | ||||
| service_name="s3", | ||||
| aws_access_key_id=config.access_key_id, | ||||
| aws_secret_access_key=config.secret_access_key, | ||||
| region_name=config.region, | ||||
| config=boto_config, | ||||
| ) | ||||
|
|
||||
| client = typing.cast(S3Client, client) | ||||
mihow marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| return client | ||||
|
|
||||
|
|
||||
|
|
@@ -124,6 +135,7 @@ def get_resource(config: S3Config) -> S3ServiceResource: | |||
| endpoint_url=config.endpoint_url, | ||||
| # api_version="s3v4", | ||||
| ) | ||||
| s3 = typing.cast(S3ServiceResource, s3) | ||||
|
||||
| s3 = typing.cast(S3ServiceResource, s3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I does not detect the correct type without this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import
boto3.sessionis not used in this file. The code usesboto3.Sessionwhich is available through the mainboto3import. This import should be removed.