Trouble accessing files via S3
Posted: Mon Jan 22, 2024 4:39 am America/New_York
Hello,
I'm trying to download a bunch of MODIS data via a python script using S3 and can't seem to get it working.
This is my script:
```
import boto3
import s3fs
token = <token from earthdata>
cred_url = "https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials"
header={"Authorization": f"Bearer {token}"}
temp_creds_req = requests.get(cred_url, headers=header).json()
fs_s3 = s3fs.S3FileSystem(anon=False,
key=temp_creds_req['accessKeyId'],
secret=temp_creds_req['secretAccessKey'],
token=temp_creds_req['sessionToken'],
client_kwargs={'region_name': 'us-west-2'})
s3_url = 's3://lp-prod-protected/MOD14.061/MOD14.A2024021.1515.061.2024021212648/MOD14.A2024021.1515.061.2024021212648.hdf'
fp = fs_s3.open(s3_url, mode='rb')
```
Which gives an error:
```
---------------------------------------------------------------------------
ClientError Traceback (most recent call last)
File ~\Anaconda3\envs\main\Lib\site-packages\s3fs\core.py:113, in _error_wrapper(func, args, kwargs, retries)
112 try:
--> 113 return await func(*args, **kwargs)
114 except S3_RETRYABLE_ERRORS as e:
File ~\Anaconda3\envs\main\Lib\site-packages\aiobotocore\client.py:383, in AioBaseClient._make_api_call(self, operation_name, api_params)
382 error_class = self.exceptions.from_code(error_code)
--> 383 raise error_class(parsed_response, operation_name)
384 else:
ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
```
Any ideas what I'm doing wrong?
I'm trying to download a bunch of MODIS data via a python script using S3 and can't seem to get it working.
This is my script:
```
import boto3
import s3fs
token = <token from earthdata>
cred_url = "https://data.lpdaac.earthdatacloud.nasa.gov/s3credentials"
header={"Authorization": f"Bearer {token}"}
temp_creds_req = requests.get(cred_url, headers=header).json()
fs_s3 = s3fs.S3FileSystem(anon=False,
key=temp_creds_req['accessKeyId'],
secret=temp_creds_req['secretAccessKey'],
token=temp_creds_req['sessionToken'],
client_kwargs={'region_name': 'us-west-2'})
s3_url = 's3://lp-prod-protected/MOD14.061/MOD14.A2024021.1515.061.2024021212648/MOD14.A2024021.1515.061.2024021212648.hdf'
fp = fs_s3.open(s3_url, mode='rb')
```
Which gives an error:
```
---------------------------------------------------------------------------
ClientError Traceback (most recent call last)
File ~\Anaconda3\envs\main\Lib\site-packages\s3fs\core.py:113, in _error_wrapper(func, args, kwargs, retries)
112 try:
--> 113 return await func(*args, **kwargs)
114 except S3_RETRYABLE_ERRORS as e:
File ~\Anaconda3\envs\main\Lib\site-packages\aiobotocore\client.py:383, in AioBaseClient._make_api_call(self, operation_name, api_params)
382 error_class = self.exceptions.from_code(error_code)
--> 383 raise error_class(parsed_response, operation_name)
384 else:
ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden
```
Any ideas what I'm doing wrong?