【Azure】微软云文件存储

文章目录

Blob文件

创建存储账户

https://portal.azure.com/#view/Microsoft_Azure_StorageHub/StorageHub.MenuView/~/StorageAccountsBrowse

创建

添加容器

填写名称

在线查看文件

使用Python读取、写入

https://learn.microsoft.com/zh-cn/python/api/overview/azure/storage-blob-readme?view=azure-python

shell 复制代码
# pip install azure-storage-blob
from pathlib import Path

from azure.storage.blob import BlobClient


class AzureStore:
    def __init__(self, connection_string: str, container_name: str, save_path: Path):
        """

        Args:
            connection_string: Azure存储凭证
            container_name: 容器名称
            save_path: 文件保存初始化目录
        """
        self.connection_string = connection_string
        self.container_name = container_name
        self.save_path = save_path
        Path(self.save_path).mkdir(parents=True, exist_ok=True)

    def upload(self, file_path: Path):
        # file_path = Path("./1.txt")
        blob = BlobClient.from_connection_string(conn_str=self.connection_string, container_name=self.container_name, blob_name=file_path.name)

        with open(file_path, "rb") as f:
            blob.upload_blob(f)

    def download(self, blob_name: str):
        blob = BlobClient.from_connection_string(conn_str=self.connection_string, container_name=self.container_name, blob_name=blob_name)

        with open(Path(self.save_path).joinpath(blob_name), "wb") as f:
            blob_data = blob.download_blob()
            blob_data.readinto(f)
if __name__ == '__main__':
    connection_string = "<访问密钥中的连接字符串>"
    store = AzureStore(connection_string, "<容器名称>", Path("./"))
    store.download("1.txt")

其他语言JAVA、Net、Spring等

JAVA:https://learn.microsoft.com/zh-cn/azure/storage/common/storage-samples-java?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json

NET:https://learn.microsoft.com/zh-cn/azure/storage/common/storage-samples-dotnet?toc=/azure/storage/blobs/toc.json&bc=/azure/storage/blobs/breadcrumb/toc.json

经典文件

同理

Python文档

https://learn.microsoft.com/zh-cn/python/api/overview/azure/storage-file-share-readme?view=azure-python

shell 复制代码
# pip install azure-storage-file-share
from pathlib import Path

from azure.storage.fileshare import ShareFileClient

"""
经典文件共享
"""
class AzureFileShareStore:
    def __init__(self, connection_string: str, share_name: str, save_path: Path):
        """

        Args:
            connection_string: Azure存储凭证
            container_name: 经典文件共享名称
            save_path: 文件保存初始化目录
        """
        self.connection_string = connection_string
        self.share_name = share_name
        self.save_path = save_path
        Path(self.save_path).mkdir(parents=True, exist_ok=True)

    def upload(self, file_path: Path, store_path: str):
        file_client = ShareFileClient.from_connection_string(conn_str=self.connection_string, share_name=self.share_name, file_path=store_path)

        with open(file_path, "rb") as f:
            file_client.upload_file(f)

    def download(self, store_path: str):
        file_client = ShareFileClient.from_connection_string(conn_str=self.connection_string, share_name=self.share_name, file_path=store_path)

        with open(Path(self.save_path).joinpath(Path(store_path).name), "wb") as f:
            data = file_client.download_file()
            data.readinto(f)


if __name__ == '__main__':
     connection_string = "<访问密钥中的连接字符串>"
    store = AzureStore(connection_string, "<经典文件创建的名称>", Path("./"))
    store.download("1.txt")
相关推荐
想你依然心痛1 天前
系统存储机制深度剖析:从Win11临时文件夹设计看微软存储架构演进
microsoft·架构
weixin_BYSJ19871 天前
springboot3家政平台小程序--附源码00904
java·javascript·spring boot·python·django·flask·php
制造数据与AI践行者老蒋2 天前
离谱!PromptTemplate 遇上 JSON,花括号直接引发解析战争
数据库·microsoft·json
行者-全栈开发2 天前
CVE-2026-45659:Microsoft SharePoint远程代码执行漏洞深度解析与修复指南
microsoft·sharepoint·反序列化·运维自动化·安全修复·远程代码执行·cve-2026-45659
第二个人2 天前
Python Web开发:从Flask到FastAPI,我经历了什么
前端·python·flask
zwd20052 天前
Manim DLL 报错,先别重装 Python|把环境问题拆成可验证的步骤
microsoft
這花開嗎3 天前
2026年TTS文字转语音API哪家强?批量自动化配音成本与技术实测
python·flask·自动化
zandy10113 天前
体验家 XMPlus 问卷答题体验优化与完成率提升引擎:从交互设计到行为心理学的工程化实践
windows·microsoft·交互
Albart5753 天前
2026年Python入门路线图:从零到实战的30天逐日详解(三)
学习·flask·#python30 天入门·python 实战教程·pytest 单元测试·python 虚拟环境·logging 日志
码银3 天前
放弃了豆包,我使用Python做了一个桌面宠物
python·microsoft·宠物