【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 小时前
从“单点收付”到“跨国司库”,金融为何是出海深水区的关键变量?
人工智能·microsoft·金融
公子小六21 小时前
基于.NET的Windows窗体编程之WinForms打印
windows·microsoft·c#·.net·winforms
知识分享小能手1 天前
Flask入门学习教程,从入门到精通,Flask智能租房——首页 知识点详解(6)
python·学习·flask
m0_634666731 天前
微软的 AI 重组和成本焦虑,正在把 Copilot 推到一场更硬的经营考试里
人工智能·microsoft·copilot
云和恩墨1 天前
数据库一体机简史:德维特与微软的“复仇者联盟”
数据库·microsoft
hzp6661 天前
PyCharm 中开发 Flask 应用时霸占 5000 端口
python·flask
㳺三才人子1 天前
初探 Flask
后端·python·flask·html
程序猿追1 天前
在 HarmonyOS 模拟器上种出斐波那契螺旋线
大数据·人工智能·microsoft·华为·harmonyos
小技与小术1 天前
玩转Flask
开发语言·python·flask