Skip to content
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

Enable anonymous S3 access by default #466

Merged
merged 5 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hats/io/file_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
find_files_matching_path,
get_directory_contents,
get_upath,
get_upath_for_protocol,
is_regular_file,
)
14 changes: 13 additions & 1 deletion src/hats/io/file_io/file_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@ def get_upath(path: str | Path | UPath) -> UPath:
return None
if isinstance(path, UPath):
return path
return UPath(path)
return get_upath_for_protocol(path)


def get_upath_for_protocol(path: str | Path) -> UPath:
"""Create UPath with protocol-specific configurations.

If we access pointers on S3 and credentials are not found we assume
an anonymous access, i.e., that the bucket is public.
"""
upath = UPath(path)
if upath.protocol == "s3":
upath = UPath(path, anon=True)
return upath


def append_paths_to_pointer(pointer: str | Path | UPath, *paths: str) -> UPath:
Expand Down
15 changes: 15 additions & 0 deletions tests/hats/catalog/loaders/test_read_hats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from hats.io.file_io import get_upath_for_protocol
from hats.loaders import read_hats


Expand All @@ -17,3 +18,17 @@ def test_read_hats_branches(
read_hats(margin_catalog_path)
read_hats(small_sky_source_dir)
read_hats(test_data_dir / "square_map")


def test_read_hats_initializes_upath_once(small_sky_dir, mocker):
mock_method = "hats.io.file_io.file_pointer.get_upath_for_protocol"
# Setting the side effect allows us to run the mocked function's code
mocked_upath_call = mocker.patch(mock_method, side_effect=get_upath_for_protocol)
read_hats(small_sky_dir)
# The construction of the UPath is called once, at the start of `read_hats`
mocked_upath_call.assert_called_once_with(small_sky_dir)


def test_read_hats_with_s3_anonymous_access():
upath = get_upath_for_protocol("s3://bucket/catalog")
assert upath.storage_options.get("anon")