【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")
相关推荐
白白白飘2 分钟前
【9】Langchain实战案例:构建一个聊天机器人
microsoft·langchain·机器人
爱奥尼欧1 小时前
【LangChain】2.提示词工程
人工智能·microsoft·langchain
每日新鲜事21 小时前
鲸蕾文化及进昂发布互动电影新作 《双生》亮相韩国富川电影节
microsoft
梦想三三1 天前
Flask + PyTorch模型部署实战:从训练权重到API接口完整工程解析(附完整代码)
人工智能·pytorch·python·flask·模型推理·ai 工程化
想会飞的蒲公英1 天前
TF-IDF + 随机森林中文文本分类全链路实战:从训练脚本到 Flask API + Streamlit 前端
人工智能·pytorch·python·随机森林·分类·flask·tf-idf
aaaa954726652 天前
2026最新5款AI编程工具平替实测合集|Claude Code低成本迭代深度对比
人工智能·microsoft
ylscode2 天前
微软推送KB5095189:Windows 11 OOBE开箱即用体验迎来新一轮优化
windows·microsoft
冰暮流星2 天前
flask之定义URL
后端·python·flask
千逐682 天前
Uniapp 鸿蒙实战之 AtomGit APP - 仓库详情与文件树浏览
服务器·microsoft·uni-app
‿hhh2 天前
Dify核心模块详解:从文本生成到智能体
人工智能·学习·microsoft·agent·上下文·记忆