diff --git a/earthaccess/api.py b/earthaccess/api.py index ccb62200..c19a1bcc 100644 --- a/earthaccess/api.py +++ b/earthaccess/api.py @@ -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 @@ -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: @@ -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 diff --git a/earthaccess/results.py b/earthaccess/results.py index 92d0b1a3..b959eca7 100644 --- a/earthaccess/results.py +++ b/earthaccess/results.py @@ -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: