Skip to main content

duckdb.set_credential_secret()

Register an access token from any TokenCredential as a DuckDB Azure secret.

Usage

Source

duckdb.set_credential_secret(
    conn, credential, account=None, scope=STORAGE_SCOPE
)

Calls credential.get_token(scope) and registers the token as set_token_secret() does. This is how an az login refresh token reaches DuckDB, which its own credential_chain cannot do.

Parameters

conn: duckdb.DuckDBPyConnection

The DuckDB connection.

credential: TokenCredential

Any TokenCredential: RefreshTokenCredential, DefaultAzureCredential(), …

account: str | None = None

Scope the secret to this storage account’s hosts, as for set_token_secret().

scope: str = STORAGE_SCOPE
The token scope.

Returns

int
The token’s expires_on, in seconds since the epoch.

Raises

TypeError

credential has no get_token() (a refresh token string, say: wrap it in RefreshTokenCredential).

ValueError

scope is empty.

azure.core.exceptions.ClientAuthenticationError
The credential could not get a token.

Notes

The secret is not refreshed in the background; call again before expires_on (typically an hour) to replace it. Delta tables never match an account-scoped secret, so register an unscoped one for them.

Examples

from dazapiz import RefreshTokenCredential, refresh_token_from_az_cli
from dazapiz.duckdb import set_credential_secret

expires_on = set_credential_secret(conn, RefreshTokenCredential(refresh_token_from_az_cli()))
conn.sql("SELECT * FROM 'abfss://data@myaccount.dfs.core.windows.net/sales/*.parquet'")