@@ -96,7 +96,7 @@ def __call__(self, bytes_transferred):
9696
9797def get_earthdata_credentials (username : Optional [str ] = None , password : Optional [str ] = None ) -> Tuple [str , str ]:
9898 """
99- Get NASA Earthdata credentials from .netrc file or direct parameters .
99+ Get NASA Earthdata credentials from direct parameters, env vars, or .netrc file .
100100 :param Optional[str] username: Directly provided username (highest priority)
101101 :param Optional[str] password: Directly provided password (highest priority)
102102 :return Tuple[str, str]: Username and password tuple
@@ -106,7 +106,23 @@ def get_earthdata_credentials(username: Optional[str] = None, password: Optional
106106 if username and password :
107107 logging .debug ("Using directly provided NASA Earthdata credentials" )
108108 return username , password
109- # Priority 2: Try to read from .netrc file
109+
110+ # Priority 2: Try to read from env vars
111+ logging .debug ("Attempting to pick up NASA Earthdata credentials from env vars..." )
112+ if all (env in _os .environ for env in ["EARTHDATA_USERNAME" , "EARTHDATA_PASSWORD" ]):
113+
114+ env_user = _os .environ ["EARTHDATA_USERNAME" ]
115+ env_pass = _os .environ ["EARTHDATA_PASSWORD" ]
116+
117+ if len (env_user ) == 0 or len (env_pass ) == 0 :
118+ raise ValueError ("NASA Earthdata username or password found in env var appears to be empty" )
119+
120+ logging .debug ("NASA Earthdata credentials successfully read from env vars" )
121+ return _os .environ ["EARTHDATA_USERNAME" ], _os .environ ["EARTHDATA_PASSWORD" ]
122+ else :
123+ logging .debug ("Env vars EARTHDATA_USERNAME or EARTHDATA_PASSWORD were not set. Trying netrc..." )
124+
125+ # Priority 3: Try to read from .netrc file
110126 try :
111127 netrc_path = _Path .home () / '.netrc'
112128 if netrc_path .exists ():
@@ -121,8 +137,11 @@ def get_earthdata_credentials(username: Optional[str] = None, password: Optional
121137 except Exception as e :
122138 logging .debug (f"Error reading .netrc: { e } " )
123139 # No credentials available
124- raise ValueError ("No NASA Earthdata credentials available. Provide username/password directly "
125- f"or set up .netrc file with entry for '{ EARTHDATA_URL } '." )
140+ raise ValueError (
141+ "No NASA Earthdata credentials available. Provide username/password directly, "
142+ "set env vars EARTHDATA_USERNAME and EARTHDATA_PASSWORD, "
143+ f"or set up .netrc file with entry for '{ EARTHDATA_URL } '."
144+ )
126145
127146
128147def upload_with_chunksize_and_meta (
0 commit comments