Python連接Azure Storage進行數(shù)據(jù)交互的實現(xiàn)
做項目的時候,數(shù)據(jù)被要求存儲在云端,需要使用Azure的Storage功能,我自己是用了Python寫的項目,所以需要調(diào)用Python接口來進行相應(yīng)的開發(fā)工作
記錄一下自己的踩坑和代碼供自己以后參考(當然Azure一直在更新,還是要不斷更新知識庫)
默認你有相應(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ǔ),默認你已經(jīng)會了)
- 再創(chuàng)建Storage Account

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

使用搜索框?qū)⒔Y(jié)果篩選為所需角色。 在此示例中,搜索“存儲 Blob 數(shù)據(jù)參與者”并選擇匹配的結(jié)果,然后選擇“下一步”。
在“訪問權(quán)限分配對象”下,選擇“用戶、組或服務(wù)主體”,然后選擇“+ 選擇成員”。
在對話框中,搜索 Azure AD 用戶名(通常為 user@domain 電子郵件地址),然后選擇對話框底部的“選擇”。
選擇“查看 + 分配”轉(zhuǎn)到最后一頁,然后再次選擇“查看 + 分配”完成該過程
2.3 Python 調(diào)用接口進行上傳和下載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進行數(shù)據(jù)交互的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python Azure Storage數(shù)據(jù)交互內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解model.train()和model.eval()兩種模式的原理與用法
這篇文章主要介紹了詳解model.train()和model.eval()兩種模式的原理與用法,相信很多沒有經(jīng)驗的人對此束手無策,那么看完這篇文章一定會對你有所幫助2023-03-03
python使用xlrd與xlwt對excel的讀寫和格式設(shè)定
最近在用python處理excel表的時候出現(xiàn)了一些問題,所以想著記錄下最后的實現(xiàn)方式和問題解決方法。方便自己或者大家在有需要的時候參考借鑒,下面這篇文章主要就介紹了python使用xlrd與xlwt對excel的讀寫和格式設(shè)定的相關(guān)資料,一起來學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
Python configparser模塊封裝及構(gòu)造配置文件
這篇文章主要介紹了Python configparser模塊封裝及構(gòu)造配置文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Python爬蟲學(xué)習(xí)之requests的使用教程
requests庫是一個常用的用于?http?請求的模塊,它使用?python?語言編寫,可以方便的對網(wǎng)頁進行爬取。本文將通過示例詳細講講requests庫的使用,需要的可以參考一下2022-08-08
python繪制y關(guān)于x的線性回歸線性方程圖像實例
這篇文章主要為大家介紹了python繪制y關(guān)于x的線性回歸線性方程圖像實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
解決Python2.7中IDLE啟動沒有反應(yīng)的問題
今天小編就為大家分享一篇解決Python2.7中IDLE啟動沒有反應(yīng)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11

