Python連接Azure Storage進(jìn)行數(shù)據(jù)交互的實(shí)現(xiàn)
做項(xiàng)目的時(shí)候,數(shù)據(jù)被要求存儲在云端,需要使用Azure的Storage功能,我自己是用了Python寫的項(xiàng)目,所以需要調(diào)用Python接口來進(jìn)行相應(yīng)的開發(fā)工作
記錄一下自己的踩坑和代碼供自己以后參考(當(dāng)然Azure一直在更新,還是要不斷更新知識庫)
默認(rèn)你有相應(yīng)的Azure的訂閱賬號,否則下面的內(nèi)容都將無法展開
1、需要的前期操作以及Python包
1、你需要安裝Azure Client然后登錄,在命令行輸入az login
2、接著安裝azure-storage-blob 、azure-identity兩個包
pip install azure-storage-blob azure-identity
2、步驟以及示例代碼
本次僅僅涉及到數(shù)據(jù)的上傳下載,一般我們會在所有流程之前先確定一個固定的資源組和Storage account,于是涉及到Python接口的部分主要是在Storage account中創(chuàng)建容器和上傳/下載blob
2.1 在Azure門戶中創(chuàng)建Storage Account
- 首先先創(chuàng)建資源組(基礎(chǔ),默認(rèn)你已經(jīng)會了)
- 再創(chuàng)建Storage Account

這里基本都是默認(rèn)設(shè)置(比較繁瑣,按下不表)
2.2 將角色分配給Azure AD用戶賬戶
這一步一開始我跳過了(因?yàn)槲乙詾橘Y源組的owner應(yīng)該是有所有權(quán)限的,其實(shí)不是),后續(xù)就遇到了上傳no permission的問題
在 Azure 門戶中,使用主搜索欄或左側(cè)導(dǎo)航找到存儲帳戶。
在存儲帳戶概述頁的左側(cè)菜單中選擇“訪問控制 (IAM)”。
在“訪問控制 (IAM)”頁上,選擇“角色分配”選項(xiàng)卡。
從頂部菜單中選擇“+ 添加”,然后從出現(xiàn)的下拉菜單中選擇“添加角色分配”。

使用搜索框?qū)⒔Y(jié)果篩選為所需角色。 在此示例中,搜索“存儲 Blob 數(shù)據(jù)參與者”并選擇匹配的結(jié)果,然后選擇“下一步”。
在“訪問權(quán)限分配對象”下,選擇“用戶、組或服務(wù)主體”,然后選擇“+ 選擇成員”。
在對話框中,搜索 Azure AD 用戶名(通常為 user@domain 電子郵件地址),然后選擇對話框底部的“選擇”。
選擇“查看 + 分配”轉(zhuǎn)到最后一頁,然后再次選擇“查看 + 分配”完成該過程
2.3 Python 調(diào)用接口進(jìn)行上傳和下載blob文件
2.3.1 創(chuàng)建container
import os, uuid
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from azure.identity import DefaultAzureCredential
account_url = "https://<storage account>.blob.core.windows.net"
default_credential = DefaultAzureCredential()
# Create the BlobServiceClient object
blob_service_client = BlobServiceClient(account_url, credential=default_credential)
# 創(chuàng)建容器
try:
# 創(chuàng)建一個叫test2的container
container_name = 'test2'
print(container_name)
# Create the container
container_client = blob_service_client.create_container(container_name)
except Exception as ex:
print('Exception:')
print(ex)
可以在Azure門戶網(wǎng)頁中看到創(chuàng)建成功

2.3.2 上傳blob
# 將blob上傳到容器中
try:
# Create a local directory to hold blob data
local_path = "./testdir1"
os.mkdir(local_path)
# Create a file in the local data directory to upload and download
local_file_name = "test2" + ".txt"
upload_file_path = os.path.join(local_path, local_file_name)
# Write text to the file
file = open(file=upload_file_path, mode='w')
file.write("Hello, World!")
file.close()
# Create a blob client using the local file name as the name for the blob
container_name="test2"
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)
# Upload the created file
with open(file=upload_file_path, mode="rb") as data:
blob_client.upload_blob(data)
except Exception as ex:
print('Exception:')
print(ex)
成功后會顯示如下:

可以用以下接口查看container中的文件
print("\nListing blobs...")
# List the blobs in the container
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)
3、參考資料
Azure Python 接口 Storage相關(guān)快速入門官方文檔
到此這篇關(guān)于Python連接Azure Storage進(jìn)行數(shù)據(jù)交互的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python Azure Storage數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5信號與槽的連接
本文講解信號與槽的連接機(jī)制,詳細(xì)示范各種類型的信號/槽連接的實(shí)現(xiàn)方法,這是圖形用戶界面的核心內(nèi)容。還將介紹面向?qū)ο蟮某绦蛟O(shè)計(jì),這是圖形用戶界面的基本思想2021-10-10
caffe的python接口生成solver文件詳解學(xué)習(xí)
這篇文章主要為大家介紹了caffe的python接口生成solver文件詳解學(xué)習(xí)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
解決Python保存文件名太長OSError: [Errno 36] File
這篇文章主要介紹了解決Python保存文件名太長OSError: [Errno 36] File name too lon問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Python實(shí)現(xiàn)希爾排序算法的原理與用法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)希爾排序算法,簡單講述了希爾排序的原理并結(jié)合具體實(shí)例形式分析了Python希爾排序的具體實(shí)現(xiàn)方法與使用技巧,需要的朋友可以參考下2017-11-11
使用Python3實(shí)現(xiàn)判斷函數(shù)的圈復(fù)雜度
編寫函數(shù)最重要的原則就是:別寫太復(fù)雜的函數(shù),那什么樣的函數(shù)才能算是過于復(fù)雜?一般會通過兩個標(biāo)準(zhǔn)來判斷,長度和圈復(fù)雜度,下面我們就來看看如何使用Python判斷函數(shù)的圈復(fù)雜度吧2024-04-04
python網(wǎng)絡(luò)爬蟲之如何偽裝逃過反爬蟲程序的方法
本篇文章主要介紹了python網(wǎng)絡(luò)爬蟲之如何偽裝逃過反爬蟲程序的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11

