此代码试图列出blob存储中的文件:
#!/usr/bin/env python3
import os
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
from datetime import datetime, timedelta
import azure.cli.core as az
print(f"Azure Blob storage v{__version__} - Python quickstart sample")
account_name = "my_account"
container_name = "my_container"
path_on_datastore = "test/path"
def _create_sas(expire=timedelta(seconds=10)) -> str:
cli = az.get_default_cli()
expire_date = datetime.utcnow() + expire
expiry_string = datetime.strftime(expire_date, "%Y-%m-%dT%H:%M:%SZ")
cmd = ["storage", "container", "generate-sas", "--name", container_name, "--account-name",
account_name, "--permissions", "lr", "--expiry", expiry_string, "--auth-mode", "login", "--as-user"]
if cli.invoke(cmd) != 0:
raise RuntimeError("Could not receive a SAS token for user {}@{}".format(
account_name, container_name))
return cli.result.result
sas = _create_sas()
blob_service_client = BlobServiceClient(
account_url=f"{account_name}.blob.core.windows.net", container_name=container_name, credential=sas)
container_client = blob_service_client.create_container(container_name)
blob_list = container_client.list_blobs()
for blob in blob_list:
print("\t" + blob.name)几周前,这段代码运行得很好,但是我们总是会发现错误:
azure.core.exceptions.ClientAuthenticationError:服务器无法验证请求。确保授权头的值是正确的,包括签名。
有人知道什么是不对的吗?
PS。使用12.3.2版本的Azure blob存储包。
编辑
出于安全考虑,我们不允许在这里使用帐户密钥。
发布于 2020-11-03 16:08:10
我不完全确定您的代码有什么问题,但是看起来您的SAS令牌并不是预期的格式。您测试过SAS在浏览器中是否有效吗?
此外,您的_create_sas函数似乎正在使用Azure命令创建SAS签名。我不认为您需要这样做,因为azure-storage-blob包有像generate_account_sas这样的方法来生成SAS签名。这将消除许多复杂性,因为您不需要担心SAS签名格式。
from datetime import datetime, timedelta
from azure.storage.blob import (
BlobServiceClient,
generate_account_sas,
ResourceTypes,
AccountSasPermissions,
)
from azure.core.exceptions import ResourceExistsError
account_name = "<account name>"
account_url = f"https://{account_name}.blob.core.windows.net"
container_name = "<container name>"
# Create SAS token credential
sas_token = generate_account_sas(
account_name=account_name,
account_key="<account key>",
resource_types=ResourceTypes(container=True),
permission=AccountSasPermissions(read=True, write=True, list=True),
expiry=datetime.utcnow() + timedelta(hours=1),
)它使SAS签名读取、写入和列出blob容器的权限,过期时间为1小时。你可以根据你的喜好改变这个。
然后,我们可以使用这个SAS签名作为凭证创建BlobServiceClient,然后创建容器客户机来列出blobs。
# Create Blob service client to interact with storage account
# Use SAS token as credential
blob_service_client = BlobServiceClient(account_url=account_url, credential=sas_token)
# First try to create container
try:
container_client = blob_service_client.create_container(name=container_name)
# If container already exists, fetch the client
except ResourceExistsError:
container_client = blob_service_client.get_container_client(container=container_name)
# List blobs in container
for blob in container_client.list_blobs():
print(blob.name)注:上面使用的是azure-storage-blob==12.5.0版本,这是最新的软件包。这与您的版本相差不远,所以我可能会更新您的代码以使用最新的功能,就像文档中所提供的那样。
更新
如果出于安全原因无法使用帐户密钥,则可以创建服务主体,并将其赋予存储帐户Storage Blob Data Contributor角色。这是作为一个AAD应用程序创建的,它可以访问您的存储帐户。
要获得这个设置,您可以从文档中使用这个指南。
示例代码
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient
token_credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(
account_url="https://<my_account_name>.blob.core.windows.net",
credential=token_credential
)发布于 2020-11-03 15:44:39
看起来这个模块被废弃了:
从v5.0.0开始,不再推荐使用“azure”元包,不能再安装了。请安装您的应用程序所需的以
azure为前缀的特定服务包。 可用包的完整列表可以在:https://aka.ms/azsdk/python/all上找到 关于这一决定的理由的更全面的讨论可在以下问题中找到:https://github.com/Azure/azure-sdk-for-python/issues/10646
https://stackoverflow.com/questions/64665157
复制相似问题