airflow icon indicating copy to clipboard operation
airflow copied to clipboard

S3Hook async methods ignore the bucket_name from the connection's service_config

Open dabla opened this issue 3 weeks ago • 4 comments

Apache Airflow Provider(s)

amazon

Versions of Apache Airflow Providers

apache-airflow-providers-amazon 9.40.0rc2 (same code on main and in 9.39.0)

Apache Airflow version

3.4.0

What happened?

provide_bucket_name fills in a missing bucket_name from different places depending on whether the decorated method is sync or async:

  • sync methods: from self.service_config["bucket_name"], i.e. extra.service_config.s3.bucket_name of the AWS connection
  • async methods (coroutines and async generators): only from connection.schema

https://github.com/apache/airflow/blob/main/providers/amazon/src/airflow/providers/amazon/aws/hooks/s3.py#L88-L127

So a connection that sets its bucket in service_config, which is what the sync methods read, gives bucket_name=None to every async method decorated with it: get_head_object_async, get_file_metadata_async, and since #75242 aload_bytes. botocore then fails in validate_bucket_name:

TypeError: expected string or bytes-like object, got 'NoneType'
  s3.py aload_bytes
  s3.py wrapper (provide_bucket_name)
  s3.py get_head_object_async
  aiobotocore/client.py _make_api_call
  botocore/handlers.py validate_bucket_name

hook.load_bytes(data, key="k") works on the same connection, but await hook.aload_bytes(data, key="k") does not.

What you think should happen instead?

Both branches should resolve the bucket the same way. The async branch could read service_config from the connection it already fetches with get_async_connection() (connection.extra_dejson["service_config"]["s3"]["bucket_name"]) instead of self.service_config, which would do a synchronous connection lookup inside the event loop. Whether connection.schema should stay as a fallback for async is open: the sync branch no longer uses it.

How to reproduce

# AWS connection "aws_s3" with extra {"service_config": {"s3": {"bucket_name": "my-bucket"}}}
from airflow.sdk import task

@task
async def upload():
    from airflow.providers.amazon.aws.hooks.s3 import S3Hook
    hook = S3Hook(aws_conn_id="aws_s3")
    await hook.aload_bytes(b"data", key="some/key")   # TypeError, bucket_name None

S3Hook(aws_conn_id="aws_s3").load_bytes(b"data", key="some/key") uploads to my-bucket.

Anything else?

Found while testing #75242 for the 9.40.0rc2 release (#76805). Happy to open the PR.

Are you willing to submit PR?

  • [x] Yes I am willing to submit a PR!

dabla avatar Nov 10 '26 18:11 dabla

Hi @dabla,

Thank you for bringing this issue to my attention and for inviting me to contribute. I appreciate your willingness to review the fix.

I've submitted PR #76855 for the related stdout logging issue (#76852), and I'd now like to take on this issue as a separate contribution.

Based on the reported behavior, the asynchronous provide_bucket_name path appears to resolve the default S3 bucket differently from the synchronous path. I'll investigate how to consistently resolve service_config.s3.bucket_name using the asynchronously retrieved connection, while preserving explicit bucket-name overrides and appropriate fallback behavior.

I'll add regression tests covering the configured default bucket, explicitly supplied bucket names, and cases where no default bucket is configured.

I'll submit a focused PR referencing #76851 once the implementation and tests are ready.

Thanks again for your support and review!

adiranjan25 avatar Nov 10 '26 18:11 adiranjan25

Hi @nguyenb2306632-cmyk,

Thanks for looking into this!

I've already opened PR #76857 to address the async bucket-resolution issue. It includes regression tests for configured buckets, schema fallback, missing configuration, explicit overrides, and missing connection IDs.

Additional coverage for other async helpers, unify_bucket_name_and_key, and documentation could be useful as a separate follow-up. I'd suggest coordinating with the maintainers to avoid overlapping changes.

Thanks for contributing!

adiranjan25 avatar Nov 19 '26 01:11 adiranjan25

Hi! I'm interested in working on this issue as my first contribution to Apache Airflow.

I'd like to investigate how the asynchronous S3Hook methods retrieve the bucket name from the connection's service_config and understand why that configuration is being ignored.

I'll first review the existing implementation and related tests, then propose a focused fix with test coverage.

Please let me know if there are any specific implementation details I should consider before proceeding.

Thank you!

Hafsa-Anwar65 avatar Nov 24 '26 16:11 Hafsa-Anwar65

Hi @Hafsa-Anwar65,

Thank you for your interest in contributing to Apache Airflow!

This issue has already been resolved through PR #76857, which was merged on November 23, 2026.

I appreciate your willingness to investigate it. I'd encourage you to explore other open issues labeled good first issue in the Apache Airflow repository.

Best wishes with your first contribution!

adiranjan25 avatar Nov 25 '26 21:11 adiranjan25