Python連接Azure Storage進(jìn)行數(shù)據(jù)交互的實(shí)現(xiàn)
做項(xiàng)目的時(shí)候,數(shù)據(jù)被要求存儲(chǔ)在云端,需要使用Azure的Storage功能,我自己是用了Python寫的項(xiàng)目,所以需要調(diào)用Python接口來進(jìn)行相應(yīng)的開發(fā)工作
記錄一下自己的踩坑和代碼供自己以后參考(當(dāng)然Azure一直在更新,還是要不斷更新知識(shí)庫)
默認(rèn)你有相應(yīng)的Azure的訂閱賬號(hào),否則下面的內(nèi)容都將無法展開
1、需要的前期操作以及Python包
1、你需要安裝Azure Client然后登錄,在命令行輸入az login
2、接著安裝azure-storage-blob 、azure-identity兩個(gè)包
pip install azure-storage-blob azure-identity
2、步驟以及示例代碼
本次僅僅涉及到數(shù)據(jù)的上傳下載,一般我們會(huì)在所有流程之前先確定一個(gè)固定的資源組和Storage account,于是涉及到Python接口的部分主要是在Storage account中創(chuàng)建容器和上傳/下載blob
2.1 在Azure門戶中創(chuàng)建Storage Account
- 首先先創(chuàng)建資源組(基礎(chǔ),默認(rèn)你已經(jīng)會(huì)了)
- 再創(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)航找到存儲(chǔ)帳戶。
在存儲(chǔ)帳戶概述頁的左側(cè)菜單中選擇“訪問控制 (IAM)”。
在“訪問控制 (IAM)”頁上,選擇“角色分配”選項(xiàng)卡。
從頂部菜單中選擇“+ 添加”,然后從出現(xiàn)的下拉菜單中選擇“添加角色分配”。
使用搜索框?qū)⒔Y(jié)果篩選為所需角色。 在此示例中,搜索“存儲(chǔ) Blob 數(shù)據(jù)參與者”并選擇匹配的結(jié)果,然后選擇“下一步”。
在“訪問權(quán)限分配對(duì)象”下,選擇“用戶、組或服務(wù)主體”,然后選擇“+ 選擇成員”。
在對(duì)話框中,搜索 Azure AD 用戶名(通常為 user@domain 電子郵件地址),然后選擇對(duì)話框底部的“選擇”。
選擇“查看 + 分配”轉(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)建一個(gè)叫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)
成功后會(huì)顯示如下:
可以用以下接口查看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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解model.train()和model.eval()兩種模式的原理與用法
這篇文章主要介紹了詳解model.train()和model.eval()兩種模式的原理與用法,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,那么看完這篇文章一定會(huì)對(duì)你有所幫助2023-03-03python使用xlrd與xlwt對(duì)excel的讀寫和格式設(shè)定
最近在用python處理excel表的時(shí)候出現(xiàn)了一些問題,所以想著記錄下最后的實(shí)現(xiàn)方式和問題解決方法。方便自己或者大家在有需要的時(shí)候參考借鑒,下面這篇文章主要就介紹了python使用xlrd與xlwt對(duì)excel的讀寫和格式設(shè)定的相關(guān)資料,一起來學(xué)習(xí)學(xué)習(xí)吧。2017-01-01Python configparser模塊封裝及構(gòu)造配置文件
這篇文章主要介紹了Python configparser模塊封裝及構(gòu)造配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08使用TensorFlow實(shí)現(xiàn)簡(jiǎn)單線性回歸模型
這篇文章主要為大家詳細(xì)介紹了使用TensorFlow實(shí)現(xiàn)簡(jiǎn)單線性回歸模型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Python實(shí)現(xiàn)朗讀在線音頻和本地音頻
在日常的Python軟件開發(fā)中,我們經(jīng)常會(huì)遇到一個(gè)非常重要的功能需求——讓程序能夠讀取并顯示文本內(nèi)容,下面我們就來學(xué)習(xí)一下Python實(shí)現(xiàn)朗讀音頻的具體操作吧2024-03-03Python爬蟲學(xué)習(xí)之requests的使用教程
requests庫是一個(gè)常用的用于?http?請(qǐng)求的模塊,它使用?python?語言編寫,可以方便的對(duì)網(wǎng)頁進(jìn)行爬取。本文將通過示例詳細(xì)講講requests庫的使用,需要的可以參考一下2022-08-08python繪制y關(guān)于x的線性回歸線性方程圖像實(shí)例
這篇文章主要為大家介紹了python繪制y關(guān)于x的線性回歸線性方程圖像實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10解決Python2.7中IDLE啟動(dòng)沒有反應(yīng)的問題
今天小編就為大家分享一篇解決Python2.7中IDLE啟動(dòng)沒有反應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11