Skip to content

Commit

Permalink
refactor API call to get s3fs sessions using search results
Browse files Browse the repository at this point in the history
  • Loading branch information
betolink committed Aug 28, 2023
1 parent 80ea1d8 commit 19170c2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
22 changes: 19 additions & 3 deletions earthaccess/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import earthaccess

from .auth import Auth
from .search import CollectionQuery, DataCollections, DataGranules, GranuleQuery
from .search import (CollectionQuery, DataCollections, DataGranules,
GranuleQuery)
from .store import Store
from .utils import _validation as validate

Expand Down Expand Up @@ -54,7 +55,10 @@ def search_datasets(
"Warning: a valid set of parameters is needed to search for datasets on CMR"
)
return []
query = DataCollections().parameters(**kwargs)
if earthaccess.__auth__.authenticated:
query = DataCollections(auth=earthaccess.__auth__).parameters(**kwargs)
else:
query = DataCollections().parameters(**kwargs)
datasets_found = query.hits()
print(f"Datasets found: {datasets_found}")
if count > 0:
Expand Down Expand Up @@ -284,13 +288,25 @@ class requests.Session: an authenticated requests Session instance.


def get_s3fs_session(
daac: Optional[str] = None, provider: Optional[str] = None
daac: Optional[str] = None,
provider: Optional[str] = None,
results: Optional[earthaccess.results.DataGranule] = None,
) -> s3fs.S3FileSystem:
"""Returns a fsspec s3fs file session for direct access when we are in us-west-2
Parameters:
daac (String): Any DAAC short name e.g. NSIDC, GES_DISC
provider (String): Each DAAC can have a cloud provider, if the DAAC is specified, there is no need to use provider
results (list[class earthaccess.results.DataGranule]): A list of results from search_data(), earthaccess will use the metadata form CMR to obtain the S3 Endpoint
Returns:
class s3fs.S3FileSystem: an authenticated s3fs session valid for 1 hour
"""
if results is not None:
endpoint = results[0].get_s3_credentials_endpoint()
if endpoint is not None:
session = earthaccess.__store__.get_s3fs_session(endpoint=endpoint)
return session
session = earthaccess.__store__.get_s3fs_session(daac=daac, provider=provider)
return session

Expand Down
6 changes: 6 additions & 0 deletions earthaccess/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ def _repr_html_(self) -> str:
granule_html_repr = _repr_granule_html(self)
return granule_html_repr

def get_s3_credentials_endpoint(self) -> Union[str, None]:
for link in self["umm"]["RelatedUrls"]:
if "/s3credentials" in link["URL"]:
return link["URL"]
return None

def size(self) -> float:
"""
Returns:
Expand Down

0 comments on commit 19170c2

Please sign in to comment.