gpt4 book ai didi

azurite - 有没有办法在启动 Azurite 时自动创建容器?

转载 作者:行者123 更新时间:2023-12-05 03:38:51 32 4
gpt4 key购买 nike

出于测试目的,我在测试管道中创建并运行了一个 Azurite docker 镜像。我希望在 Azurite 启动后自动创建 blob 容器,因为它会简化事情。

有什么好的方法可以实现吗?

对于我们使用的 Postgres 镜像,我们可以指定一个在启动时运行的 init.sql。如果 Azurite 有类似的东西,那就太棒了。

最佳答案

您可以使用以下 Dockerfile 安装 azure-storage-blob基于 Alpine 的 azurite 图像上的 Python 包。与 ~1.2GB azure-cli 图像相比,生成的图像大小约为 400MB。

ARG AZURITE_VERSION="3.17.0"
FROM mcr.microsoft.com/azure-storage/azurite:${AZURITE_VERSION}

# Install azure-storage-blob python package
RUN apk update && \
apk --no-cache add py3-pip && \
apk add --virtual=build gcc libffi-dev musl-dev python3-dev && \
pip3 install --upgrade pip && \
pip3 install azure-storage-blob==12.12.0

# Copy init_azurite.py script
COPY ./init_azurite.py init_azurite.py

# Copy local blobs to azurite
COPY ./init_containers init_containers

# Run the blob emulator and initialize the blob containers
CMD python3 init_azurite.py --directory=init_containers & \
azurite-blob --blobHost 0.0.0.0 --blobPort 10000

init_azurite.py 脚本是一个本地 Python 脚本,它使用 azure-storage-blob 包将文件和目录批量上传到 azurite blob 存储模拟器。

import argparse
import os
from time import sleep

from azure.core.exceptions import ResourceExistsError
from azure.storage.blob import BlobServiceClient, ContainerClient


def upload_file(container_client: ContainerClient, source: str, dest: str) -> None:
"""
Upload a single file to a path inside the container.
"""
print(f"Uploading {source} to {dest}")
with open(source, "rb") as data:
try:
container_client.upload_blob(name=dest, data=data)
except ResourceExistsError:
pass


def upload_dir(container_client: ContainerClient, source: str, dest: str) -> None:
"""
Upload a directory to a path inside the container.
"""
prefix = "" if dest == "" else dest + "/"
prefix += os.path.basename(source) + "/"
for root, dirs, files in os.walk(source):
for name in files:
dir_part = os.path.relpath(root, source)
dir_part = "" if dir_part == "." else dir_part + "/"
file_path = os.path.join(root, name)
blob_path = prefix + dir_part + name
upload_file(container_client, file_path, blob_path)

def init_containers(
service_client: BlobServiceClient, containers_directory: str
) -> None:
"""
Iterate on the containers directory and do the following:
1- create the container.
2- upload all folders and files to the container.
"""
for container_name in os.listdir(containers_directory):
container_path = os.path.join(containers_directory, container_name)
if os.path.isdir(container_path):
container_client = service_client.get_container_client(container_name)
try:
container_client.create_container()
except ResourceExistsError:
pass
for blob in os.listdir(container_path):
blob_path = os.path.join(container_path, blob)
if os.path.isdir(blob_path):
upload_dir(container_client, blob_path, "")
else:
upload_file(container_client, blob_path, blob)


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Initialize azurite emulator containers."
)
parser.add_argument(
"--directory",
required=True,
help="""
Directory that contains subdirectories named after the
containers that we should create. Each subdirectory will contain the files
and directories of its container.
"""
)

args = parser.parse_args()

# Connect to the localhost emulator (after 5 secs to make sure it's up).
sleep(5)
blob_service_client = BlobServiceClient(
account_url="http://localhost:10000/devstoreaccount1",
credential={
"account_name": "devstoreaccount1",
"account_key": (
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq"
"/K1SZFPTOtr/KBHBeksoGMGw=="
)
}
)

# Only initialize if not already initialized.
if next(blob_service_client.list_containers(), None):
print("Emulator already has containers, will skip initialization.")
else:
init_containers(blob_service_client, args.directory)

此脚本将被复制到 azurite 容器,并会在每次启动 azurite 容器时填充初始 blob 容器,除非某些容器已经使用 docker 卷持久化。在那种情况下,什么也不会发生。

以下是一个示例 docker-compose.yml 文件:

azurite:
build:
context: ./
dockerfile: Dockerfile
args:
AZURITE_VERSION: 3.17.0
restart: on-failure
ports:
- 10000:10000
volumes:
- azurite-data:/opt/azurite

volumes:
azurite-data:

使用此类卷将保留模拟器数据,直到您销毁它们(例如,通过使用 docker-compose down -v)。

最后,init_containers 是包含容器及其文件夹/文件的本地目录。构建镜像时,它会被复制到 azurite 容器中。

例如:

init_containers:
container-name-1:
dir-1:
file.txt
img.png
dir-2:
file.txt
container-name-2:
dir-1:
file.txt
img.png

关于azurite - 有没有办法在启动 Azurite 时自动创建容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68834959/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com