Skip to main content

Open a private dataset with xarray

A code snippet to describe how to open a dataset in a private bucket with xarray, allowing for optimize data access

Context


To open a dataset located in a private bucket, you need to use s3fs to access your bucket as a file system. Then, you can point to your file:

import s3fs
from os import environ
import xarray

fs = s3fs.S3FileSystem(
client_kwargs={'endpoint_url': 'https://'+environ["AWS_S3_ENDPOINT"]},
key = environ["AWS_ACCESS_KEY_ID"],
secret = environ["AWS_SECRET_ACCESS_KEY"],
token = environ["AWS_SESSION_TOKEN"]
)

ds = xarray.open_dataset(fs.open("<BUCKET_NAME>/<DATASET_NAME>"))
print(ds)

What's next?


If you have any questions, problems, or suggestions, please feel free to contact us via chat using the widget available at the bottom right of the page.

Did this answer your question?