Skip to content

Commit

Permalink
Merge pull request #2364 from camptocamp/contaner-url-GSCMG-24
Browse files Browse the repository at this point in the history
Add support of container URL
  • Loading branch information
sbrunner authored Jun 18, 2024
2 parents 67ed280 + b10ac9d commit fd9bc50
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
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

0 comments on commit fd9bc50

Please sign in to comment.