Create SAS service using python

Chilakapati Jyothirmayi 0 Reputation points
2024-06-07T06:03:57.13+00:00

In powerbi, Is it possible to create sas service for a blob using python?
https://video2.skills-academy.com/en-us/azure/storage/blobs/sas-service-create-python

Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,871 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Sudipta Chakraborty - MSFT 1,101 Reputation points Microsoft Employee
    2024-06-07T06:21:05.94+00:00

    @Chilakapati Jyothirmayi : You can create a SAS token using Python and use it in Power BI to connect to a Azure storage account. While configuring the data source in Power BI choose the Authentication kind as Shared Access Signature (SAS).

    The below mentioned code creates a SAS token that’s valid for one day and grants read access to the specified blob. Please replace blob_client and account_key with your actual blob client and account key.

    from azure.storage.blob import BlobServiceClient, BlobClient, BlobSasPermissions, generate_blob_sas
    import datetime
    def create_service_sas_blob(blob_client: BlobClient, account_key: str):
        # Create a SAS token that's valid for one day
        sas_token = generate_blob_sas(
            blob_client.account_name,
            blob_client.container_name,
            blob_client.blob_name,
            account_key=account_key,
            permission=BlobSasPermissions(read=True),
            expiry=datetime.datetime.utcnow() + datetime.timedelta(days=1)
        )
        return sas_token
    
    0 comments No comments

  2. Nehruji R 4,131 Reputation points Microsoft Vendor
    2024-06-10T06:00:47.17+00:00

    Hello Chilakapati Jyothirmayi,

    Greetings! Welcome to Microsoft Q&A Platform.

    Yes, In Power BI you can create a service Shared Access Signature (SAS) for Azure Blob Storage using Python. A shared access signature (SAS) enables you to grant limited access to containers and blobs in your storage account. When you create a SAS, you specify its constraints, including which Azure Storage resources a client is allowed to access, what permissions they have on those resources, and how long the SAS is valid.

    You can create a service SAS to delegate limited access to a blob resource using the following method:

    The storage account access key used to sign the SAS is passed to the method as the account_key argument. Allowed permissions are passed to the method as the permission argument, and are defined in the BlobSasPermissions class.

    The following code example shows how to create a service SAS with read permissions for a blob resource:

    def create_service_sas_blob(self, blob_client: BlobClient, account_key: str):
        # Create a SAS token that's valid for one day, as an example
        start_time = datetime.datetime.now(datetime.timezone.utc)
        expiry_time = start_time + datetime.timedelta(days=1)
    
        sas_token = generate_blob_sas(
            account_name=blob_client.account_name,
            container_name=blob_client.container_name,
            blob_name=blob_client.blob_name,
            account_key=account_key,
            permission=BlobSasPermissions(read=True),
            expiry=expiry_time,
            start=start_time
        )
    
        return sas_token
    
    

    You can find more information on how to create a SAS service for a Blob using Python in the following links - https://video2.skills-academy.com/en-us/azure/storage/blobs/sas-service-create-python, https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/storage/blobs/sas-service-create-python.md

    Hope this answer helps! Please let us know if you have any further queries. I’m happy to assist you further.


    Please "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments