【发布时间】:2023-03-10 09:05:02
【问题描述】:
我正在尝试在 Azure 中创建一个存储帐户,并使用他们的 python SDK 将 blob 上传到其中。 我设法创建了一个这样的帐户:
client = get_client_from_auth_file(StorageManagementClient)
storage_account = client.storage_accounts.create(
resourceGroup,
name,
StorageAccountCreateParameters(
sku=Sku(name=SkuName.standard_ragrs),
enable_https_traffic_only=True,
kind=Kind.storage,
location=region)).result()
问题是稍后我正在尝试构建一个容器,但我不知道要插入什么作为“account_url” 我试过这样做:
client = get_client_from_auth_file(BlobServiceClient, account_url=storage_account.primary_endpoints.blob)
return client.create_container(name)
但我得到:
azure.core.exceptions.ResourceNotFoundError: The specified resource does not exist
我确实设法使用以下方法创建了一个容器:
client = get_client_from_auth_file(StorageManagementClient)
return client.blob_containers.create(
resourceGroup,
storage_account.name,
name,
BlobContainer(),
public_access=PublicAccess.Container
)
但后来当我尝试使用 BlobServiceClient 或 BlobClien 上传 blob 时,我仍然需要“account_url”,所以我仍然收到错误:
azure.core.exceptions.ResourceNotFoundError: The specified resource does not exist
任何人都可以帮助我了解如何获取我使用 SDK 创建的存储帐户的 account_url?
编辑: 通过从存储密钥创建连接字符串,我设法找到了解决该问题的方法。
storage_client = get_client_from_auth_file(StorageManagementClient)
storage_keys = storage_client.storage_accounts.list_keys(resource_group, account_name)
storage_key = next(v.value for v in storage_keys.keys)
return BlobServiceClient.from_connection_string(
'DefaultEndpointsProtocol=https;' +
f'AccountName={account_name};' +
f'AccountKey={storage_key};' +
'EndpointSuffix=core.windows.net')
这可行,但我认为 George Chen 的答案更优雅。
【问题讨论】:
-
你能打印出
client你从这行得到的client = get_client_from_auth_file(BlobServiceClient, account_url=storage_account.primary_endpoints.blob)吗? -
是的,如果我打印
client.account_name,我会得到<azure.storage.blob._blob_service_client.BlobServiceClient object at 0x103218be0>,这是正确的名称
标签: python azure azure-storage azure-blob-storage