Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of container URL #2364

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions tilecloud_chain/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ruamel.yaml
from azure.core.exceptions import ResourceNotFoundError
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, ContentSettings
from azure.storage.blob import BlobServiceClient, ContainerClient, ContentSettings
from bottle import jinja2_template
from PIL import Image
from prometheus_client import Summary
Expand Down Expand Up @@ -104,15 +104,22 @@ def main(args: Optional[list[str]] = None, out: Optional[IO[str]] = None) -> Non
sys.exit(1)


def get_azure_client() -> BlobServiceClient:
def get_azure_container_client(container: str) -> ContainerClient:
"""Get the Azure blog storage client."""
if "AZURE_STORAGE_CONNECTION_STRING" in os.environ and os.environ["AZURE_STORAGE_CONNECTION_STRING"]:
return BlobServiceClient.from_connection_string(os.environ["AZURE_STORAGE_CONNECTION_STRING"])
return BlobServiceClient.from_connection_string(
os.environ["AZURE_STORAGE_CONNECTION_STRING"]
).get_container_client(container=container)
elif "AZURE_STORAGE_BLOB_CONTAINER_URL" in os.environ:
container_client = ContainerClient.from_container_url(os.environ["AZURE_STORAGE_BLOB_CONTAINER_URL"])
if os.environ.get("AZURE_STORAGE_BLOB_VALIDATE_CONTAINER_NAME", "true").lower() == "true":
assert container == container_client.container_name
return container_client
else:
return BlobServiceClient(
account_url=os.environ["AZURE_STORAGE_ACCOUNT_URL"],
credential=DefaultAzureCredential(),
)
).get_container_client(container=container)


def _send(
Expand All @@ -134,7 +141,8 @@ def _send(
if cache["type"] == "azure":
cache_azure = cast(tilecloud_chain.configuration.CacheAzure, cache)
key_name = os.path.join(f"{cache['folder']}", path)
blob = get_azure_client().get_blob_client(container=cache_azure["container"], blob=key_name)
container = get_azure_container_client(cache_azure["container"])
blob = container.get_blob_client(key_name)
blob.upload_blob(data, overwrite=True)

blob.upload_blob(
Expand Down Expand Up @@ -177,7 +185,9 @@ def _get(path: str, cache: tilecloud_chain.configuration.Cache) -> Optional[byte
cache_azure = cast(tilecloud_chain.configuration.CacheAzure, cache)
key_name = os.path.join(f"{cache['folder']}", path)
try:
blob = get_azure_client().get_blob_client(container=cache_azure["container"], blob=key_name)
blob = get_azure_container_client(container=cache_azure["container"]).get_blob_client(
blob=key_name
)
return blob.download_blob().readall()
except ResourceNotFoundError:
return None
Expand Down
8 changes: 4 additions & 4 deletions tilecloud_chain/server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2013-2023 by Stéphane Brunner
# Copyright (c) 2013-2024 by Stéphane Brunner
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -53,7 +53,7 @@
import tilecloud_chain.security
from tilecloud import Tile, TileCoord
from tilecloud_chain import TileGeneration, configuration, controller, internal_mapcache
from tilecloud_chain.controller import get_azure_client
from tilecloud_chain.controller import get_azure_container_client

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -281,8 +281,8 @@ def _get(
key_name = os.path.join(cache_azure["folder"], path)
try:
with _GET_TILE.labels(storage="azure").time():
blob = get_azure_client().get_blob_client(
container=cache_azure["container"], blob=key_name
blob = get_azure_container_client(container=cache_azure["container"]).get_blob_client(
blob=key_name
)
properties = blob.get_blob_properties()
data = blob.download_blob().readall()
Expand Down
Loading