在本出版物中,我们将看到用于 Azure Blob 存储的主要方法

pip install azure-storage-blob

进入全屏模式 退出全屏模式

Blob 服务客户端

from os import getenv
from azure.storage.blob import BlobServiceClient

blob_service_client = BlobServiceClient.from_connection_string(
    getenv("AZURE_STORAGE_CONNECTION_STRING"))

进入全屏模式 退出全屏模式

连接字符串

[连接字符串 Azure Blob 存储](https://res.cloudinary.com/practicaldev/image/fetch/s--Ej_jkf0G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev -to-uploads.s3.amazonaws.com/uploads/articles/fhx57qlvofcwst3z09ly.png)

用于 blob 的方法(文件)

上传 Blob

def upload_blob(filename: str, container: str, data: BinaryIO):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        blob_client.upload_blob(data)

        print("success")
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

下载 Blob

def download_blob(filename: str, container: str):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        print(blob_client.download_blob().readall())
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

删除 Blob

def delete_blob(filename: str, container: str):
    try:
        blob_client = blob_service_client.get_blob_client(
            container=container, blob=filename)

        blob_client.delete_blob()

        print("success")
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

容器方法(文件夹)

创建容器

def create_container(container: str):
    try:
        blob_service_client.create_container(container)
        print("success")
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

删除容器

def delete_container(container: str):
    try:
        blob_service_client.delete_container(container)
        print("success")
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

列出容器

def get_containers():
    try:
        containers = blob_service_client.list_containers()
        print([container.name for container in containers])
    except Exception as e:
        print(e.message)

进入全屏模式 退出全屏模式

Github 要点

https://gist.github.com/nelsoncode019/eade5071c80f659bfa7ce9a452345d85

Logo

学AI,认准AI Studio!GPU算力,限时免费领,邀请好友解锁更多惊喜福利 >>>

更多推荐