【问题标题】:Azure: create storage account with container and upload blob to it in PythonAzure:使用容器创建存储帐户并在 Python 中将 blob 上传到其中
【发布时间】: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


【解决方案1】:

我可以重现这个问题,然后我发现get_client_from_auth_file 无法将凭据传递给BlobServiceClient,因为如果只是使用account_url 创建BlobServiceClient 而没有凭据,它也可以打印帐户名称。

所以如果你想使用凭证来获取BlobServiceClient,你可以使用下面的代码,然后进行其他操作。

credentials = ClientSecretCredential(
    'tenant_id',
    'application_id',
    'application_secret'
)

blobserviceclient=BlobServiceClient(account_url=storage_account.primary_endpoints.blob,credential=credentials)

如果您不想这样,您可以使用帐户密钥创建BlobServiceClient

client = get_client_from_auth_file(StorageManagementClient,auth_path='auth')

storage_account = client.storage_accounts.create(
        'group name',
        'account name',
        StorageAccountCreateParameters(
            sku=Sku(name=SkuName.standard_ragrs),
            enable_https_traffic_only=True,
            kind=Kind.storage,
            location='eastus',)).result()
storage_keys = client.storage_accounts.list_keys(resource_group_name='group name',account_name='account name')
storage_keys = {v.key_name: v.value for v in storage_keys.keys}
blobserviceclient=BlobServiceClient(account_url=storage_account.primary_endpoints.blob,credential=storage_keys['key1'])
blobserviceclient.create_container(name='container name')

【讨论】:

  • 我设法通过从帐户名称和存储密钥创建连接字符串来找到解决该问题的另一种解决方法,但您的解决方案看起来更优雅。我在帖子中添加我的解决方案以进行比较。谢谢!
猜你喜欢
  • 2021-07-24
  • 2020-11-02
  • 2018-01-19
  • 2022-11-11
  • 2022-11-30
  • 2021-06-11
  • 2019-03-08
  • 2011-02-24
  • 2016-02-17
相关资源
最近更新 更多