-
Notifications
You must be signed in to change notification settings - Fork 1
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
DM-42522: Implement exists() and size() for S3 presigned URLs #77
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
``HttpResourcePath.exists()`` and ``HttpResourcePath.size()`` now work for S3 HTTP URLs pre-signed for GET. |
This file contains 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 |
---|---|---|
|
@@ -14,9 +14,10 @@ | |
__all__ = ("HttpReadResourceHandle",) | ||
|
||
import io | ||
import re | ||
from collections.abc import Callable, Iterable | ||
from logging import Logger | ||
from typing import AnyStr | ||
from typing import AnyStr, NamedTuple | ||
|
||
import requests | ||
from lsst.utils.timer import time_this | ||
|
@@ -203,17 +204,13 @@ def read(self, size: int = -1) -> bytes: | |
# in the file and also the current position we have got to in the | ||
# server. | ||
if "Content-Range" in resp.headers: | ||
content_range = resp.headers["Content-Range"] | ||
units, range_string = content_range.split(" ") | ||
if units == "bytes": | ||
range, total = range_string.split("/") | ||
if "-" in range: | ||
_, end = range.split("-") | ||
end_pos = int(end) | ||
if total != "*" and end_pos >= int(total) - 1: | ||
self._eof = True | ||
else: | ||
self._log.warning("Requested byte range from server but instead got: %s", content_range) | ||
content_range = parse_content_range_header(resp.headers["Content-Range"]) | ||
if ( | ||
content_range.total is not None | ||
and content_range.range_end is not None | ||
and content_range.range_end >= content_range.total - 1 | ||
): | ||
self._eof = True | ||
|
||
# Try to guess that we overran the end. This will not help if we | ||
# read exactly the number of bytes to get us to the end and so we | ||
|
@@ -223,3 +220,60 @@ def read(self, size: int = -1) -> bytes: | |
|
||
self._current_position += len_content | ||
return resp.content | ||
|
||
|
||
class ContentRange(NamedTuple): | ||
"""Represents the data in an HTTP Content-Range header.""" | ||
|
||
range_start: int | None | ||
"""First byte of the zero-indexed, inclusive range returned by this | ||
response. `None` if the range was not available in the header. | ||
""" | ||
range_end: int | None | ||
"""Last byte of the zero-indexed, inclusive range returned by this | ||
response. `None` if the range was not available in the header. | ||
""" | ||
total: int | None | ||
"""Total size of the file in bytes. `None` if the file size was not | ||
available in the header. | ||
""" | ||
|
||
|
||
def parse_content_range_header(header: str) -> ContentRange: | ||
"""Parse an HTTP 'Content-Range' header. | ||
|
||
Parameters | ||
---------- | ||
header : `str` | ||
Value of an HTTP Content-Range header to be parsed. | ||
|
||
Returns | ||
------- | ||
content_range : `ContentRange` | ||
The byte range included in the response and the total file size. | ||
|
||
Raises | ||
------ | ||
ValueError | ||
If the header was not in the expected format. | ||
""" | ||
# There are three possible formats for Content-Range. All of them start | ||
# with optional whitespace and a unit, which for our purposes should always | ||
# be "bytes". | ||
prefix = r"^\s*bytes\s+" | ||
|
||
# Content-Range: <unit> <range-start>-<range-end>/<size> | ||
if (case1 := re.match(prefix + r"(\d+)-(\d+)/(\d+)", header)) is not None: | ||
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. Hopefully three separate regexes won't turn into a hotspot. |
||
return ContentRange( | ||
range_start=int(case1.group(1)), range_end=int(case1.group(2)), total=int(case1.group(3)) | ||
) | ||
|
||
# Content-Range: <unit> <range-start>-<range-end>/* | ||
if (case2 := re.match(prefix + r"(\d+)-(\d+)/\*", header)) is not None: | ||
return ContentRange(range_start=int(case2.group(1)), range_end=int(case2.group(2)), total=None) | ||
|
||
# Content-Range: <unit> */<size> | ||
if (case3 := re.match(prefix + r"\*/(\d+)", header)) is not None: | ||
return ContentRange(range_start=None, range_end=None, total=int(case3.group(1))) | ||
|
||
raise ValueError(f"Content-Range header in unexpected format: '{header}'") |
This file contains 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
This file contains 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
This file contains 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.
There's a slight behavior change here. Previously if the units were anything but
bytes
it would only log a warning. Now it throws an exception. This should never happen in real life, and if it does the returned data is almost certainly wrong so we want to bail anyway.