Cloud region affinity (earthlens.base.region)#
region_affinity(bucket_region) answers one question: is this process running in the same cloud region as the
target bucket? A backend uses the answer to choose between a cheap in-region streaming read and a slow,
billed cross-region egress download.
It returns one of three verdicts:
| Verdict | Meaning |
|---|---|
"in-region" |
the caller region equals bucket_region — stream directly |
"egress" |
caller and bucket are in different (known) regions — expect billed, slower transfer |
"unknown" |
the caller region could not be determined — fall back to the safe (HTTPS) path |
How the caller region is resolved#
Resolution is env-first, then an optional metadata probe:
- an explicit
caller_region=argument, then AWS_REGION/AWS_DEFAULT_REGION, then- (only when
probe=True) an instance-metadata probe — AWS IMDSv2, GCPmetadata.google.internal, or Azure IMDS — each behind a hard 1 s timeout and cached per process.
Detection never raises and never blocks: any failure yields "unknown". Pass probe=False to skip all
network access entirely (tests, air-gapped runs). The earthdata backend deliberately uses probe=False so its
in-region decision never risks hanging an off-cloud caller.
Warning before large egress#
warn_if_egress(bucket_region, *, size_bytes, threshold_bytes=1<<30) emits a single loguru warning when the
hint is "egress" and the transfer exceeds the threshold (default 1 GiB), and returns the hint. The s3
backend calls it before a bulk pull (using the object sizes the listing already reported), so a cross-region
transfer is never a silent cost surprise.
from earthlens.base.region import region_affinity, warn_if_egress
if region_affinity("us-west-2") == "in-region":
... # stream from S3 in-region
else:
warn_if_egress("us-west-2", size_bytes=total_bytes) # advise, then HTTPS download
API#
earthlens.base.region.region_affinity(bucket_region, *, caller_region=None, probe=True)
#
Classify the caller's region relative to a target bucket.
Resolves the caller region in order — explicit caller_region, then
AWS_REGION / AWS_DEFAULT_REGION, then (when probe) an
instance-metadata probe — and compares it to bucket_region. The
result guides a backend's choice between an in-region streaming read
and a cross-region egress download.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_region
|
str | None
|
The region the target bucket lives in (e.g.
|
required |
caller_region
|
str | None
|
An explicit override for the caller's region. When given, the environment and probe are not consulted. |
None
|
probe
|
bool
|
Whether to fall back to the instance-metadata probe when
the environment carries no region. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
|
str
|
|
str
|
the caller region cannot be determined (or |
str
|
empty). |
Examples:
- An explicit caller region drives the verdict without touching the environment:
Source code in src/earthlens/base/region.py
earthlens.base.region.warn_if_egress(bucket_region, *, size_bytes, threshold_bytes=DEFAULT_EGRESS_THRESHOLD_BYTES, caller_region=None, probe=True)
#
Warn once before a large cross-region pull, returning the hint.
Computes the region affinity and, when the caller is in a different
region than the bucket ("egress") and the transfer exceeds
threshold_bytes, emits a single loguru warning so the user learns
about the cross-region cost and latency up front. The hint is
returned so a caller can branch on it too.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket_region
|
str | None
|
The region the target bucket lives in. |
required |
size_bytes
|
int
|
The size of the pending transfer in bytes. |
required |
threshold_bytes
|
int
|
The size above which the warning fires (default 1 GiB). A parameter, never hard-coded at the call site. |
DEFAULT_EGRESS_THRESHOLD_BYTES
|
caller_region
|
str | None
|
An explicit override for the caller's region. |
None
|
probe
|
bool
|
Whether to allow the instance-metadata probe (see
:func: |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The affinity hint — |