Skip to main content

Wraps Azure SDK to keep common operations short and consistent.

Project description

kleiven-azure-storage

Small helpers around Azure Table Storage and Blob Storage clients. The package wraps the Azure SDK to keep common operations short and consistent.

pip install kleiven-azure-storage

Requirements

  • Python 3.10+
  • azure-data-tables
  • azure-storage-blob
  • python-dotenv

Configuration

The package loads environment variables via python-dotenv. Define your Azure connection string in .env or the shell:

AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=...;

You can also pass a custom env var name to the helpers.

Quick start

Tables

from kleiven.azure.storage import get_table_storage

users = get_table_storage(table_name="users", env_variable_name = "AZURE_STORAGE_CONNECTION_STRING")

# Get an entity
entity = users.get_entity(
        partition_key = 'users',
        row_key = "123",
        select = 'Name') # None if it partition_key, row_key does not exist in table

if entity is not None:
  print(entity.get("Name", None))



# Upsert entity (replace by default)
entity = {
    "PartitionKey": "users",
    "RowKey": "123",
    "name": "Ada",
}

# Upsert entity (replace by default)
table.upsert_entity(entity)

# Upsert entity: Merge
from azure.data.tables import UpdateMode
storage.upsert_entity(
    entity=entity,
    mode=UpdateMode.MERGE,
    create_table_if_not_exist = True
)

# Create table if not already exist
storage.upsert_entity(
    entity=entity,
    create_table_if_not_exist = True
)

Blobs

from kleiven.azure.storage import get_blob_service

service = get_blob_service()
blob = service.create_and_upload_blob(
    data={"hello": "world"},
    container="my-container",
    blob_name="example.json",
    tags={"source": "demo"},
    overwrite=True,
)

if blob is not None:
    print(blob.container, blob.blob)
    print(blob.metadata)

API overview

Helper functions

  • get_table_service(env_variable_name="AZURE_STORAGE_CONNECTION_STRING")
  • get_table_storage(table_name, env_variable_name="AZURE_STORAGE_CONNECTION_STRING")
  • get_blob_service(env_variable_name="AZURE_STORAGE_CONNECTION_STRING")
  • get_blob_storage(container, blob, env_variable_name="AZURE_STORAGE_CONNECTION_STRING")

TableStorage

TableStorage wraps azure.data.tables.TableClient:

  • upsert_entity(entity, mode=UpdateMode.REPLACE, create_table_if_not_exist=False)
  • get_entity(partition_key, row_key, select=None, silent=True, create_table_if_not_exist=False)
  • query_entities(query_filter, select=None)
  • get_entities(entities, select=None)
  • batch_save(entities, update_mode=UpdateMode.MERGE, create_table_if_not_exist=False)
  • batch_delete(entities)

BlobService and BlobStorage

BlobService wraps azure.storage.blob.BlobServiceClient:

  • get_blob_storage(container, blob)
  • create_and_upload_blob(data, container, blob_name, tags=None, overwrite=False)

BlobStorage exposes convenience properties:

  • container
  • blob
  • metadata

Notes

  • create_and_upload_blob returns None when overwrite=False and the blob already exists.
  • create_table_if_not_exist=True currently depends on a wolfbrain.storage helper. If you do not have that dependency, keep the flag set to False.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kleiven_azure_storage-0.0.1.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kleiven_azure_storage-0.0.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file kleiven_azure_storage-0.0.1.tar.gz.

File metadata

  • Download URL: kleiven_azure_storage-0.0.1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kleiven_azure_storage-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a20d457e445168d47286f00ee015cb6cbc8ce43a5d2fc433c07e0aa37fe576c0
MD5 42377ed5376104e17038ed63041169d1
BLAKE2b-256 1f0bd74e6b3ce7f2ba5328a1ed55f7644aed2a532ecb19a4f6ed1c203d97a8d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for kleiven_azure_storage-0.0.1.tar.gz:

Publisher: publish-to-pypi.yml on kleiven/kleiven-azure-storage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file kleiven_azure_storage-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for kleiven_azure_storage-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3a4abd5c048b926929d5390a2d63a0e08d607fa55a2036c80aa5db7ac987e837
MD5 7b97291f7ed7f88e1b62561e77e4688b
BLAKE2b-256 5d411d3078fa89da950c719f0e54a9a141fc2faa3259474bd272d66e97f098d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for kleiven_azure_storage-0.0.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on kleiven/kleiven-azure-storage

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page