0

I'm trying to create a storage account in Azure and upload a blob into it using their python SDK. I managed to create an account like this:

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()

The problem is that later I'm trying to build a container and I don't know what to insert as "account_url" I have tried doing:

client = get_client_from_auth_file(BlobServiceClient, account_url=storage_account.primary_endpoints.blob)
return client.create_container(name)

But I'm getting:

azure.core.exceptions.ResourceNotFoundError: The specified resource does not exist

I did manage to create a container using:

client = get_client_from_auth_file(StorageManagementClient)
return client.blob_containers.create(
    resourceGroup,
    storage_account.name,
    name,
    BlobContainer(),
    public_access=PublicAccess.Container
)

But later when I'm trying to upload a blob using BlobServiceClient or BlobClien I still need the "account_url" so I'm still getting an error:

azure.core.exceptions.ResourceNotFoundError: The specified resource does not exist

Anyone can help me to understand how do I get the account_url for a storage account I created with the SDK?

EDIT: I managed to find a workaround to the problem by creating the connection string from the storage keys.

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')

This works but I thin George Chen answer is more elegant.

2
  • 1
    Can you print client you got from this line client = get_client_from_auth_file(BlobServiceClient, account_url=storage_account.primary_endpoints.blob)? Commented May 5, 2020 at 11:45
  • Yes, I get <azure.storage.blob._blob_service_client.BlobServiceClient object at 0x103218be0> btw if I print client.account_name it is the right name Commented May 5, 2020 at 12:03

1 Answer 1

2

I could reproduce this problem, then I found get_client_from_auth_file could not pass the credential to the BlobServiceClient, cause if just create BlobServiceClient with account_url without credential it also could print the account name.

So if you want to use a credential to get BlobServiceClient, you could use the below code, then do other operations.

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

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

If you don't want this way, you could create the BlobServiceClient with the account key.

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')
Sign up to request clarification or add additional context in comments.

1 Comment

I managed to find another workaround to the problem by creating the connection string from the account name and the storage keys but your solution looks more elegant. I'm Adding my solution in the post for comparison. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.