-
Notifications
You must be signed in to change notification settings - Fork 21
Sign vrt-strings #33
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
Merged
Merged
Sign vrt-strings #33
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import re | ||
from datetime import datetime, timezone | ||
from typing import Any, Dict, Optional | ||
import warnings | ||
|
@@ -11,7 +12,13 @@ | |
from pystac_client import ItemSearch | ||
|
||
from planetary_computer.settings import Settings | ||
from planetary_computer.utils import parse_blob_url, parse_adlfs_url, is_fsspec_asset | ||
from planetary_computer.utils import ( | ||
parse_blob_url, | ||
parse_adlfs_url, | ||
is_fsspec_asset, | ||
is_vrt_string, | ||
asset_xpr, | ||
) | ||
|
||
|
||
BLOB_STORAGE_DOMAIN = ".blob.core.windows.net" | ||
|
@@ -73,13 +80,41 @@ def sign(obj: Any) -> Any: | |
|
||
|
||
@sign.register(str) | ||
def sign_string(url: str) -> str: | ||
"""Sign a URL or VRT-like string containing URLs with a Shared Access (SAS) Token | ||
|
||
Signing with a SAS token allows read access to files in blob storage. | ||
|
||
Args: | ||
url (str): The HREF of the asset as a URL or a GDAL VRT | ||
|
||
Single URLs can be found on a STAC Item's Asset ``href`` value. Only URLs to | ||
assets in Azure Blob Storage are signed, other URLs are returned unmodified. | ||
|
||
GDAL VRTs can combine many data sources into a single mosaic. A VRT can be | ||
built quickly from the GDAL STACIT driver | ||
https://gdal.org/drivers/raster/stacit.html. Each URL to Azure Blob Storage | ||
within the VRT is signed. | ||
|
||
Returns: | ||
str: The signed HREF or VRT | ||
""" | ||
if is_vrt_string(url): | ||
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. For the record, this doesn't seem to have a measurable effect on the timing of # main, after signing a URL to get a cached token
In [5]: %timeit planetary_computer.sign(url)
12.2 µs ± 1.97 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
# HEAD
In [4]: %timeit planetary_computer.sign(url)
11.3 µs ± 1.7 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each) |
||
return sign_vrt_string(url) | ||
else: | ||
return sign_url(url) | ||
|
||
|
||
def sign_url(url: str) -> str: | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Sign a URL with a Shared Access (SAS) Token, which allows for read access. | ||
"""Sign a URL or with a Shared Access (SAS) Token | ||
|
||
Signing with a SAS token allows read access to files in blob storage. | ||
|
||
Args: | ||
url (str): The HREF of the asset in the format of a URL. | ||
This can be found on STAC Item's Asset 'href' value. Only URLs to assets | ||
in Azure Blob Storage are signed, other URLs are returned unmodified. | ||
url (str): The HREF of the asset as a URL | ||
|
||
Single URLs can be found on a STAC Item's Asset ``href`` value. Only URLs to | ||
assets in Azure Blob Storage are signed, other URLs are returned unmodified. | ||
|
||
Returns: | ||
str: The signed HREF | ||
|
@@ -93,6 +128,46 @@ def sign_url(url: str) -> str: | |
return token.sign(url).href | ||
|
||
|
||
def _repl_vrt(m: re.Match) -> str: | ||
# replace all blob-storages URLs with a signed version. | ||
return sign_url(m.string[slice(*m.span())]) | ||
|
||
|
||
def sign_vrt_string(vrt: str) -> str: | ||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Sign a VRT-like string containing URLs with a Shared Access (SAS) Token | ||
|
||
Signing with a SAS token allows read access to files in blob storage. | ||
|
||
Args: | ||
vrt (str): The GDAL VRT | ||
|
||
GDAL VRTs can combine many data sources into a single mosaic. A VRT can be | ||
built quickly from the GDAL STACIT driver | ||
https://gdal.org/drivers/raster/stacit.html. Each URL to Azure Blob Storage | ||
within the VRT is signed. | ||
|
||
Returns: | ||
str: The signed VRT | ||
|
||
Examples | ||
-------- | ||
>>> from osgeo import gdal | ||
>>> from pathlib import Path | ||
>>> search = ( | ||
... "STACIT:\"https://planetarycomputer.microsoft.com/api/stac/v1/search?" | ||
... "collections=naip&bbox=-100,40,-99,41" | ||
... "&datetime=2019-01-01T00:00:00Z%2F..\":asset=image" | ||
... ) | ||
>>> gdal.Translate("out.vrt", search) | ||
>>> signed_vrt = planetary_computer.sign(Path("out.vrt").read_text()) | ||
>>> print(signed_vrt) | ||
<VRTDataset rasterXSize="161196" rasterYSize="25023"> | ||
... | ||
</VRTDataset> | ||
""" | ||
return asset_xpr.sub(_repl_vrt, vrt) | ||
|
||
|
||
@sign.register(Item) | ||
def sign_item(item: Item) -> Item: | ||
"""Sign all assets within a PySTAC item | ||
|
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
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__version__ = "0.4.3" | ||
"""Library version""" | ||
|
||
__version__ = "0.4.4" |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
black==20.8b1 | ||
flake8==3.8.4 | ||
black==21.10b0 | ||
flake8==4.0.1 | ||
ipdb==0.13.7 | ||
mypy==0.790 | ||
setuptools==56.0.0 | ||
mypy==0.910 | ||
types-requests==2.26.0 | ||
setuptools==58.5.3 |
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.
Uh oh!
There was an error while loading. Please reload this page.