【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")
相关推荐
TechWayfarer17 小时前
苏超赛事网站安全防护:WAF、DDoS与仿冒页面如何联动治理
网络·python·安全·flask·ddos
c++之路17 小时前
备忘录模式(Memento Pattern)
c++·microsoft
vortex517 小时前
新手前后端开发学习指南:从Flask框架到全栈实践
后端·python·flask
Solis程序员18 小时前
MCP (Model Context Protocol):AI应用连接外部世界的标准协议
人工智能·microsoft·agent·skill·mcp
诺未科技_NovaTech18 小时前
上海诺未携手惠灵顿中国,基于微软 Azure 打造 AI 教育生态标杆
人工智能·microsoft·azure·ai教育
hnult19 小时前
在线笔试平台如何选型?考试云九重防作弊 + 六大 AI 能力 智能招聘笔试解决方案
人工智能·笔记·microsoft·信息可视化·课程设计
叫我:松哥20 小时前
基于Python flask的中学可控智能命题系统设计与实现,整合遗传算法、DeepSeek 大模型及数据库技术构建一体化应用
数据库·人工智能·python·算法·机器学习·flask·遗传算法
步步为营DotNet20 小时前
Blazor 与 Microsoft.Extensions.AI 在客户端性能优化中的协同应用
人工智能·microsoft·性能优化
chushiyunen1 天前
langchain4j笔记、tools
笔记·python·flask
叫我:松哥1 天前
基于机器学习的中文文本抑郁症风险检测系统,包括NLP与传统机器学习的抑郁症识别,准确率92%
人工智能·深度学习·机器学习·自然语言处理·flask·nlp·bootstrap