From 3fc9a7efce7bb95ba3f931428e78199d5c3f3bb5 Mon Sep 17 00:00:00 2001 From: Vincent Tran Date: Wed, 19 Oct 2022 14:21:53 -0700 Subject: [PATCH 1/3] Fixed duplicate sample tag, added missing sample to async + unify samples to match more closely --- .../samples/blob_samples_authentication.py | 5 +-- .../blob_samples_authentication_async.py | 31 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py index 9bb370da57525..28712fb7e44f5 100644 --- a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py +++ b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py @@ -122,7 +122,7 @@ def auth_shared_access_signature(self): # [END create_sas_token] def auth_default_azure_credential(self): - # [START create_blob_service_client_oauth] + # [START create_blob_service_client_oauth_default_credential] # Get a credential for authentication # Default Azure Credentials attempt a chained set of authentication methods, per documentation here: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity # For example user (who must be an Azure Event Hubs Data Owner role) to be logged in can be specified by the environment variable AZURE_USERNAME @@ -137,13 +137,14 @@ def auth_default_azure_credential(self): account_url=self.oauth_url, credential=default_credential ) - # [END create_blob_service_client_oauth] + # [END create_blob_service_client_oauth_default_credential] # Get account information for the Blob Service account_info = blob_service_client.get_service_properties() if __name__ == '__main__': sample = AuthSamples() + # Uncomment the methods you want to execute. sample.auth_connection_string() sample.auth_active_directory() sample.auth_shared_access_signature() diff --git a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py index eb8d9ae28e04f..d18aba1541cf6 100644 --- a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py +++ b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py @@ -84,8 +84,11 @@ async def auth_active_directory_async(self): # [START create_blob_service_client_oauth] # Get a token credential for authentication from azure.identity.aio import ClientSecretCredential - token_credential = ClientSecretCredential(self.active_directory_tenant_id, self.active_directory_application_id, - self.active_directory_application_secret) + token_credential = ClientSecretCredential( + self.active_directory_tenant_id, + self.active_directory_application_id, + self.active_directory_application_secret + ) # Instantiate a BlobServiceClient using a token credential from azure.storage.blob.aio import BlobServiceClient @@ -111,11 +114,31 @@ async def auth_shared_access_signature_async(self): ) # [END create_sas_token] + async def auth_default_azure_credential(self): + # [START create_blob_service_client_oauth_default_credential] + # Get a credential for authentication + # Default Azure Credentials attempt a chained set of authentication methods, per documentation here: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity + # For example user (who must be an Azure Event Hubs Data Owner role) to be logged in can be specified by the environment variable AZURE_USERNAME + # Alternately, one can specify the AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET to use the EnvironmentCredentialClass. + # The docs above specify all mechanisms which the defaultCredential internally support. + from azure.identity.aio import DefaultAzureCredential + default_credential = DefaultAzureCredential() + + # Instantiate a BlobServiceClient using a token credential + from azure.storage.blob.aio import BlobServiceClient + blob_service_client = BlobServiceClient( + account_url=self.oauth_url, + credential=default_credential + ) + # [END create_blob_service_client_oauth_default_credential] + + # Get account information for the Blob Service + account_info = blob_service_client.get_service_properties() + async def main(): sample = AuthSamplesAsync() - # Uncomment the methods you want to execute. await sample.auth_connection_string_async() - # await sample.auth_active_directory() + await sample.auth_active_directory_async() await sample.auth_shared_access_signature_async() await sample.auth_blob_url_async() From dbe733babcdd53e0433abda14ba4a00681bdac4e Mon Sep 17 00:00:00 2001 From: Vincent Tran Date: Wed, 19 Oct 2022 14:26:45 -0700 Subject: [PATCH 2/3] Add function call to newest sample in async --- .../samples/blob_samples_authentication_async.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py index d18aba1541cf6..1077d87fdadd5 100644 --- a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py +++ b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication_async.py @@ -141,6 +141,7 @@ async def main(): await sample.auth_active_directory_async() await sample.auth_shared_access_signature_async() await sample.auth_blob_url_async() + await sample.auth_default_azure_credential() if __name__ == '__main__': asyncio.run(main()) From db6b16dd6104f2d1f6cd88cd8f72cc2bf14b8216 Mon Sep 17 00:00:00 2001 From: Vincent Tran Date: Wed, 19 Oct 2022 14:36:58 -0700 Subject: [PATCH 3/3] Removed uncomment comment --- .../azure-storage-blob/samples/blob_samples_authentication.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py index 28712fb7e44f5..918dcc2c861bc 100644 --- a/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py +++ b/sdk/storage/azure-storage-blob/samples/blob_samples_authentication.py @@ -144,7 +144,6 @@ def auth_default_azure_credential(self): if __name__ == '__main__': sample = AuthSamples() - # Uncomment the methods you want to execute. sample.auth_connection_string() sample.auth_active_directory() sample.auth_shared_access_signature()