-
Notifications
You must be signed in to change notification settings - Fork 5
✨ Extend UPath.to_url() to private paths
#1379
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
Open
sheetalgiri
wants to merge
5
commits into
main
Choose a base branch
from
upath_to_url_extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−36
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e147c4
Extend UPath.to_url() so that it returns laminhub /storage url for pr…
sheetalgiri b0f6cec
Add tests for to_url extension
sheetalgiri e6d0cb6
Raise unsupported error for GCS path that is not public
sheetalgiri 83ba441
ruff
sheetalgiri 2abd84f
Updated tests
sheetalgiri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -531,8 +531,8 @@ def synchronize_to( | |
| # no need to cast local_stat.st_mtime to int | ||
| # because if it has the fractional part and cloud_mtime doesn't | ||
| # and they have the same integer part then cloud_mtime can't be bigger | ||
| is_sync_needed = ( | ||
| lambda cloud_mtime, local_stat: cloud_mtime > local_stat.st_mtime | ||
| is_sync_needed = lambda cloud_mtime, local_stat: ( | ||
| cloud_mtime > local_stat.st_mtime | ||
| ) | ||
|
|
||
| local_paths: list[Path] = [] | ||
|
|
@@ -808,27 +808,56 @@ def view_tree( | |
| logger.print(message) | ||
|
|
||
|
|
||
| def to_url(upath: S3Path) -> str: | ||
| """Public storage URL. | ||
| def to_url(upath: UPath) -> str: | ||
| """Generates a URL for an object represented by `UPath`. | ||
|
|
||
| Generates a public URL for an object in an S3 bucket using fsspec's UPath, | ||
| considering the bucket's region. | ||
| For S3/GCS paths, this returns a public URL considering the bucket region. | ||
| If the S3 path is not publicly hosted, it returns a LaminHub URL if the artifact is hosted on LaminHub. | ||
|
|
||
| Args: | ||
| upath: A `UPath` object representing an S3 path. | ||
| upath: A `UPath` object. | ||
|
|
||
| Returns: | ||
| A string containing the public URL to the S3 object. | ||
| A string containing the URL to the object. | ||
| """ | ||
| if upath.protocol != "s3": | ||
| raise ValueError("The provided UPath must be an S3 path.") | ||
| key = "/".join(upath.parts[1:]) | ||
| bucket = upath.drive | ||
| region = get_storage_region(upath) | ||
| if region == "us-east-1": | ||
| return f"https://{bucket}.s3.amazonaws.com/{key}" | ||
| else: | ||
| return f"https://{bucket}.s3-{region}.amazonaws.com/{key}" | ||
| from ._settings import settings | ||
|
|
||
| if upath.protocol == "s3": | ||
| key = "/".join(upath.parts[1:]) | ||
| bucket = upath.drive | ||
| if _is_publicly_accessible_path(upath): | ||
| region = get_storage_region(upath) | ||
| if region == "us-east-1": | ||
| return f"https://{bucket}.s3.amazonaws.com/{key}" | ||
| return f"https://{bucket}.s3-{region}.amazonaws.com/{key}" | ||
| elif settings.instance.is_on_hub: | ||
| origin = settings.instance.ui_url | ||
| if origin is not None: | ||
| common = f"{origin}/storage/s3/{bucket}%2F/{key}" | ||
| return common | ||
| else: | ||
| raise ValueError( | ||
| "The provided S3 UPath must be publicly accessible or the artifact must be hosted on LaminHub." | ||
| ) | ||
| if upath.protocol == "gs": | ||
| if _is_publicly_accessible_path(upath): | ||
| return f"https://storage.googleapis.com/{str(upath).removeprefix('gs://')}" | ||
| else: | ||
| raise ValueError( | ||
| "This function only supports publicly accessible GCS paths." | ||
| ) | ||
| if upath.protocol in {"http", "https"}: | ||
| return str(upath) | ||
| raise ValueError("The provided UPath must be an S3, GCS, HTTP, or HTTPS path.") | ||
|
|
||
|
|
||
| def _is_publicly_accessible_path(upath: UPath) -> bool: | ||
| """Check whether an S3/GCS path is anonymously readable.""" | ||
| anon_path = UPath(upath, anon=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if i remember correctly, anon=True works only for s3.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it is s3fs specific argument |
||
| try: | ||
| return anon_path.exists() | ||
| except Exception: | ||
| return False | ||
|
|
||
|
|
||
| def from_auth(cls, path: AnyPathStr) -> UPath: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_on_hubis not the correct thing to check, this just checks that the instance is registered on the hub, ie this is also true for cloud sqlite instances.is_managed_by_hubis the right thing to check. Ho