Skip to content

Commit

Permalink
feat: add support for S3 Extra arguments in Seed file upload to s3
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kervizic committed Sep 5, 2023
1 parent ded2747 commit 8a7d4ff
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ A dbt profile can be configured to run against AWS Athena using the following co
| aws_profile_name | Profile to use from your AWS shared credentials file. | Optional | `my-profile` |
| work_group | Identifier of Athena workgroup | Optional | `my-custom-workgroup` |
| num_retries | Number of times to retry a failing query | Optional | `3` |
| extra_args | Dictionary containing boto3 ExtraArgs when uploading to S3 | Optional | `{"ACL": "bucket-owner-full-control"}`|

**Example profiles.yml entry:**
```yaml
Expand Down
2 changes: 2 additions & 0 deletions dbt/adapters/athena/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class AthenaCredentials(Credentials):
num_retries: Optional[int] = 5
s3_data_dir: Optional[str] = None
s3_data_naming: Optional[str] = "schema_table_unique"
extra_args: Optional[Dict[str, Any]] = None

@property
def type(self) -> str:
Expand All @@ -83,6 +84,7 @@ def _connection_keys(self) -> Tuple[str, ...]:
"s3_data_dir",
"s3_data_naming",
"debug_query_state",
"extra_args"
)


Expand Down
3 changes: 2 additions & 1 deletion dbt/adapters/athena/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def upload_seed_to_s3(
s3_data_dir: Optional[str] = None,
s3_data_naming: Optional[str] = None,
external_location: Optional[str] = None,
extra_args: Optional[Dict[str, Any]] = None
) -> str:
conn = self.connections.get_thread_connection()
client = conn.handle
Expand All @@ -315,7 +316,7 @@ def upload_seed_to_s3(
# This ensures cross-platform support, tempfile.NamedTemporaryFile does not
tmpfile = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
table.to_csv(tmpfile, quoting=csv.QUOTE_NONNUMERIC)
s3_client.upload_file(tmpfile, bucket, object_name)
s3_client.upload_file(tmpfile, bucket, object_name, ExtraArgs=extra_args)
os.remove(tmpfile)

return str(s3_location)
Expand Down
2 changes: 2 additions & 0 deletions dbt/include/athena/macros/materializations/seeds/helpers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
{%- set s3_data_dir = config.get('s3_data_dir', default=target.s3_data_dir) -%}
{%- set s3_data_naming = config.get('s3_data_naming', target.s3_data_naming) -%}
{%- set external_location = config.get('external_location', default=none) -%}
{%- set extra_args = config.get('extra_args', default=target.extra_args) -%}

{%- set tmp_relation = api.Relation.create(
identifier=identifier + "__dbt_tmp",
Expand All @@ -110,6 +111,7 @@
s3_data_dir,
s3_data_naming,
external_location,
extra_args=extra_args
) -%}

-- create target relation
Expand Down
3 changes: 3 additions & 0 deletions dbt/include/athena/profile_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ prompts:
hint: Specify the database (Data catalog) to build models into (lowercase only)
default: awsdatacatalog

extra_args:
hint: Specify any extra arguments to use in the S3 Upload, e.g. ACL, SSEKMSKeyId

threads:
hint: '1 or more'
type: 'int'
Expand Down

0 comments on commit 8a7d4ff

Please sign in to comment.