Skip to content

Commit

Permalink
Fix support of container URL
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jul 2, 2024
1 parent 87606fb commit 400eb1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
24 changes: 23 additions & 1 deletion tilecloud_chain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import c2cwsgiutils.setup_process
import jsonschema_validator
import psycopg2
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, ContainerClient, ContentSettings
from c2cwsgiutils import sentry
from PIL import Image
from prometheus_client import Counter, Summary
Expand Down Expand Up @@ -379,6 +381,26 @@ def filter(self, record: Any) -> bool:
return True


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"]
).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
), f"Container name mismatch: {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)


class TileGeneration:
"""Base class of all the tile generation."""

Expand Down Expand Up @@ -802,9 +824,9 @@ def get_store(
cache_azure = cast(tilecloud_chain.configuration.CacheAzureTyped, cache)
# on Azure
cache_tilestore = AzureStorageBlobTileStore(
container=cache_azure["container"],
tilelayout=layout,
cache_control=cache_azure.get("cache_control"),
client=get_azure_container_client(cache_azure["container"]),
)
elif cache["type"] == "mbtiles":
metadata = {}
Expand Down
22 changes: 2 additions & 20 deletions tilecloud_chain/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
import requests
import ruamel.yaml
from azure.core.exceptions import ResourceNotFoundError
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, ContainerClient, ContentSettings
from azure.storage.blob import ContentSettings
from bottle import jinja2_template
from PIL import Image
from prometheus_client import Summary
Expand All @@ -32,6 +31,7 @@
TileGeneration,
add_common_options,
configuration,
get_azure_container_client,
get_queue_store,
get_tile_matrix_identifier,
)
Expand Down Expand Up @@ -104,24 +104,6 @@ def main(args: Optional[list[str]] = None, out: Optional[IO[str]] = None) -> Non
sys.exit(1)


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"]
).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(
data: Union[bytes, str], path: str, mime_type: str, cache: tilecloud_chain.configuration.Cache
) -> None:
Expand Down
9 changes: 7 additions & 2 deletions tilecloud_chain/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@
import tilecloud_chain.configuration
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_container_client
from tilecloud_chain import (
TileGeneration,
configuration,
controller,
get_azure_container_client,
internal_mapcache,
)

_LOGGER = logging.getLogger(__name__)

Expand Down

0 comments on commit 400eb1a

Please sign in to comment.