I got 403 error when attempting to iterate over blobs in my Azure Storage container using the container_client.list_blobs() method

Eden Hazani 20 Reputation points
2024-05-29T14:34:35.54+00:00

I am encountering a 403 error when attempting to iterate over blobs in my Azure Storage container using the container_client.list_blobs() method. The error occurs specifically when entering the for loop. Here is the relevant part of my code:

# option 1:
def get_folders_from_last_month(self, container_client):
        today = datetime.today()
        current_year_month = today.strftime('%Y_%m')

        try:
            blobs = container_client.walk_blobs(name_starts_with=current_year_month)
            logging.info('Listing blobs with prefix %s', current_year_month)

            folders = set()
            for blob in blobs:
                logging.info('Processing blob: %s', blob.name)
                folder = blob.name.split('/')[0]
                if self.is_valid_date(folder):
                    folders.add(folder)
            return folders
        except Exception as e:
            logging.error('Error occurred: %s', str(e))
            raise
 # option 2:
def get_folders(self, container_client):
    try:
        blobs = container_client.list_blobs()
        logging.info('blobs %s', blobs)
        folders = set()
        for blob in blobs:
            folder = blob.name.split('/')[0]
            logging.info('folder %s', folder)
            if self.is_valid_date(folder):
                folders.add(folder)
        return folders
    except Exception as e:
        logging.error('Error occurred: %s', str(e))
        raise


I have ensured that my credentials and permissions are correct. I have tried connecting to the container using both a SAS token and a connection string, but the issue persists. Any guidance on resolving this would be appreciated.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,507 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,558 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amrinder Singh 3,795 Reputation points Microsoft Employee
    2024-05-29T14:42:06.8666667+00:00

    Hi Eden Hazani - Thanks for reaching out.

    If you have validated the SAS and connection string to be correct but still facing a 403, I would suggest you to review the networking configurations once.

    Please verify if you have allowed access from All the Networks or there is some restriction w.r.t. specific IP, VNET etc? If there are, please flip to allowing access from all the networks and then test it once. This shall help with the isolation.

    Let me know how it goes ahead and if this was helpful.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful