[学习记录]Azure Blob Storage :Upload blobs to a container
import os,uuid
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient,BlobClient,ContainerClient
#DefaultAzureCredential:在代码中实现与Azure服务的无密码链接
try:
#connect your app code to Azure using DefaultAzureCredential
print("Azure Blob Storage Python quickstart sample")
account_url="https://xxxxx.blob.core.windows.net"
default_credential = DefaultAzureCredential()
#create a object of BlobServiceClient
#the BlobServiceClient class allows you to manipuate Azure Storage resources and blob containers
blob_service_client=BlobServiceClient(account_url,credential=default_credential)
#Create a container
container_name = "xxxxxxxx"
container_client = blob_service_client.create_container(container_name)##failedEnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
#
#create a local directory to hold blod date
local_path = './data'
os.mkdir(local_path)
#create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".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 local file name as the name for the blob
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)
#list the blobs in the container
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)
#clean up
print("\nPress the Enter key to begin clean up.")
input()
print("Deleting the Blob container...")
container_client.delete_container()
#
print("Deleting the local sources...")
os.remove(upload_file_path)
os.rmdir(local_path)
except Exception as ex:
print("Exception:")
print(ex)
error:
Azure Blob Storage Python quickstart sample
DefaultAzureCredential failed to retrieve a token from the included credentials.
Attempted credentials:
EnvironmentCredential: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.
ManagedIdentityCredential: ManagedIdentityCredential authentication unavailable, no response from the IMDS endpoint.
SharedTokenCacheCredential: SharedTokenCacheCredential authentication unavailable. No accounts were found in the cache.
AzureCliCredential: Azure CLI not found on path
AzurePowerShellCredential: Failed to invoke PowerShell
AzureDeveloperCliCredential: Azure Developer CLI could not be found. Please visit https://aka.ms/azure-dev for installation instructions and then,once installed, authenticate to your Azure account using 'azd auth login'.
To mitigate this issue, please refer to the troubleshooting guidelines here at https://aka.ms/azsdk/python/identity/defaultazurecredential/troubleshoot.
解决:
1.在Azure portal 的Access Control (IAM)上赋予用户Storage Blob Data Contributor权限
2.在local安装Azure CLI后,在VSCODE中运行 az login 命令登录到Azure。
3.执行代码
其他:
①application 链接到Azure Blob Storage
可以通过Azure Identity client library的DefaultAzureCredential class来实现无密码链接。
②在本地开发时,为了确保用户能够读写blob data,需要赋予用户Storage Blob Data Contributor的权限。
参考资料:Quickstart: Azure Blob Storage client library for Python
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: