Error When Listing and Downloading Blobs from Azure Storage Account Using SAS Token

Manuj Shivam Sharma 0 Reputation points
2024-11-28T09:32:53.06+00:00

I am trying to connect to a specific container in my Azure Storage Account and read the files. My goal is to list all the blobs in the container, download each blob into an in-memory byte stream, save the stream contents to a local Parquet file, and then read the Parquet file into a pandas DataFrame.

Here is the code I am using:

from azure.storage.blob import BlobServiceClient, ContainerClient
from io import BytesIO
import pandas as pd


container_name = 'poc-powerapp-container'


sas_token = 'your_sas_token'
blob_service_url = 'your_blob_service_url' 


blob_service_client = BlobServiceClient(account_url=blob_service_url, credential=sas_token)
container_client = blob_service_client.get_container_client(container_name)


blobs_list = container_client.list_blobs()

for blob in blobs_list:


    if 'parquet' in blob.name.lower():
        try:


            blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob.name)
            

            stream = BytesIO()
            

            blob_client.download_blob().readinto(stream)
            

            stream.seek(0)
            

            local_parquet_file_path = "local_file_path.parquet"
            with open(local_parquet_file_path, 'wb') as f:
                f.write(stream.getbuffer())
                
            print(f"Parquet file saved as '{local_parquet_file_path}'")
            


            df = pd.read_parquet(local_parquet_file_path)
            print(df.head())
            
        except Exception as e:
            print(f"Error processing blob {blob.name}: {e}")

However, I am encountering the following error:

HttpResponseError: The requested URI does not represent any resource on the server.

HttpResponseError                         Traceback (most recent call last)
...
File ~\AppData\Roaming\Python\Python312\site-packages\azure\storage\blob\_list_blobs_helper.py:96, in BlobPropertiesPaged._get_next_cb(self, continuation_token)
     89     return self._command(
     90         prefix=self.prefix,
     91         marker=continuation_token or None,
     92         maxresults=self.results_per_page,
     93         cls=return_context_and_deserialized,
     94         use_location=self.location_mode)
     95 except HttpResponseError as error:
---> 96     process_storage_error(error)

What I've Tried

  • Verified the SAS token and Blob service URL.
  • Checked the permissions for the storage account and container.
  • Ensured that the blobs exist in the container.

Questions

  1. What could be causing the HttpResponseError: The requested URI does not represent any resource on the server error?
  2. Is there something wrong with the way I am listing or accessing the blobs?
  3. Are there any additional steps I should take to debug this issue?

Any help or guidance would be greatly appreciated!

Thank you!

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.
3,301 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,006 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Vinod Kumar Reddy Chilupuri 1,915 Reputation points Microsoft Vendor
    2024-11-29T16:39:11.57+00:00

    Hi @Manuj Shivam Sharma
    I understand that you're still encountering the error, here are some troubleshooting steps that may help you to resolve your error. "HttpResponseError: The requested URI does not represent any resource on the server."

    1. Add Logging: Log the exact URLs you are trying to access to find any issues.
    2. Test a Known Blob: Try accessing a blob you know exists to see if the problem is with your code or the blob itself.
    3. Regenerate SAS Token: If you think the SAS token might be the issue, create a new one with the right permissions.
    4. Check Blob Names: Make sure blob names don’t have special characters or are too long, as these can cause problems.
    5. Use Azure Storage Explorer: Check the blobs in the container visually and try downloading one to see if it works.
    6. Check Network Settings: Ensure your network allows connections to Azure Blob Storage. Try running your code on a different network to see if it still fails.
    7. Monitor Azure Status: Check the Azure Status Page for any ongoing issues with Azure Blob Storage.
    8. Review Azure Logs: Look at Azure logs for any errors or failed requests related to your storage account for more information.

    https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python?tabs=managed-identity%2Croles-azure-portal%2Csign-in-azure-cli&pivots=blob-storage-quickstart-scratch#download-blobs

    https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download-python?source=recommendations

    Please feel free to contact if the issue persists, we will be glad to assist you closely.

    0 comments No comments

  2. Amrinder Singh 5,555 Reputation points Microsoft Employee
    2024-12-02T05:27:35.3933333+00:00

    Hi Manuj Shivam Sharma - Thanks for reaching out over Q&A Forum.

    Based on the exception message, the request URL seem to be getting mal-formed and hence when it is being presented to the storage server, it is not able to recognize the same.

    You should try to print the URI that get created before uploading to Storage. Below is reference sample that you can check for uploading the blob from local

    https://github.com/Azure-Developer-Support/CodeRepository/blob/master/Storage/Python/BlobServiceClientUploadSample.py

    Hope that helps!

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


  3. Sumarigo-MSFT 47,371 Reputation points Microsoft Employee
    2024-12-03T09:10:59.01+00:00

    @Manuj Shivam Sharma Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    The error is pretty clear that you seems to have added an incorrect value to the variables in your application.

    This syntax of how the values for the variables should look like:

    For the BlobServiceClient function, the account url parameter should look like below:

    account_url="https://<my-storage-account-name>.blob.core.windows.net/"

    The sas token should look something like below:

    sas_token = 'sv=2024-12-03&ss=b&srt=sco&sp=rl&se=2024-12-04T00:00:00Z&st=2024-12-03T00:00:00Z&spr=https&sig=wejfseufeurfberfberfberfb='
    

    The blob_service_url should look like below: 

    blob_service_url = 'https://exampleaccount.blob.core.windows.net'

    Please let us know if you have any further queries. I’m happy to assist you further.    


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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.