Skip to content

Commit 711051f

Browse files
authored
fix(blob): force virtual-hosted regional S3 URL with explicit endpoint_url (#63)
Setting region_name + signature_version=s3v4 alone wasn't enough — boto3 was still emitting the legacy bucket.s3.amazonaws.com URL. Adding endpoint_url + addressing_style=virtual forces the regional URL the browser can actually fetch without CORS-blocking redirect.
1 parent 1b09449 commit 711051f

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

src/blob_storage.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,24 @@ def _key(package_id: str) -> str:
2525

2626

2727
def _client():
28-
"""boto3 S3 client pinned to the function's region with sigv4.
29-
30-
Without these, boto3 generates URLs like `bucket.s3.amazonaws.com`
31-
which 308-redirect to the regional endpoint for non-us-east-1 buckets.
32-
Browsers treat the redirect as cross-origin and CORS-block the response.
33-
"""
28+
"""boto3 S3 client pinned to a regional endpoint with sigv4 + virtual
29+
addressing — so presigned URLs come out as
30+
`bucket.s3.<region>.amazonaws.com/...` directly. Without all three,
31+
boto3 emits the legacy global `bucket.s3.amazonaws.com` URL, which
32+
S3 308-redirects to the regional endpoint, and browsers CORS-block
33+
the redirect."""
3434
import boto3
3535
from botocore.config import Config
3636

37+
region = os.environ.get("AWS_REGION", "eu-west-1")
3738
return boto3.client(
3839
"s3",
39-
region_name=os.environ.get("AWS_REGION", "eu-west-1"),
40-
config=Config(signature_version="s3v4"),
40+
region_name=region,
41+
endpoint_url=f"https://s3.{region}.amazonaws.com",
42+
config=Config(
43+
signature_version="s3v4",
44+
s3={"addressing_style": "virtual"},
45+
),
4146
)
4247

4348

0 commit comments

Comments
 (0)