Skip to content

Azure Blob Storage

This example demonstrates how to use the Azure Blob Storage connector in ydata-sdk.

Don't forget to set up your license key

    import os

    os.environ['YDATA_LICENSE_KEY'] = '{add-your-key}'

Example Code

"""Azure Blob Storage connector example."""
from pathlib import Path

from examples.local import setting_dask_env
from ydata.connectors.filetype import FileType
from ydata.connectors.storages.azure_blob_connector import AzureBlobConnector
from ydata.utils.formats import read_json

setting_dask_env()


def get_token(token_name: str):
    "Utility to load a token from .secrets"
    # Use relative path from file to token to be able to run regardless of the cwd()
    token_path = (
        Path(__file__).absolute().parent.parent.parent.joinpath(
            ".secrets", token_name)
    )
    return read_json(token_path)


if __name__ == "__main__":

    # Load the secret token
    token = get_token("azure_credentials.json")

    # Initiatialize the connector
    connector = AzureBlobConnector(**token)

    # List all the existing containers
    containers = connector.list()

    # List all the existing blobs inside a container
    blobs = connector.list(path="abfs://path-to-file")

    # Read a file
    data = connector.read_file(
        path="https://path-to-file/data.csv",
        file_type=FileType.CSV,
    )

    # Read a sample of the file
    data = connector.read_sample(
        path="abfs://path-to-file/data.csv", file_type=FileType.CSV
    )

    # Write a new file
    connector.write_file(data, path="abfs://path-to-file/test.csv")