'BlobServiceClient' object has no attribute 'ls_files'

Question:

I am trying to use the function ls_files from BlobServiceClient library found in this github (https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-blob/samples/blob_samples_directory_interface.py). However, when I ran the code, I get an error:

‘BlobServiceClient’ object has no attribute ‘ls_files’

Here is my code:

import os, uuid, sys
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient, PublicAccess, __version__
from azure.storage.blob.blockblobservice import BlockBlobService
import re

account_name = ACCOUNT_NAME
account_key = ACCOUNT_KEY
connect_str = CONNECTION_STRING
account_url = ACCOUNT_URL
container_name = CONTAINER_NAME
file_path = FILE_PATH
block_blob_service = BlockBlobService(account_name = account_name, account_key = account_key)
blob_service_client = BlobServiceClient(account_url = account_url, connect_str = connect_str, container_name = container_name)

def list_filenames_in_blob(blob):
    file_names = blob_service_client.ls_files(file_path)
    return file_names

def run_action():
    try:
       for blob in generator:
            list_filenames_in_blob(blob)
except Exception as e:
        print(e)

# Main method.
if __name__ == '__main__':
    run_action()

Could you please let me know what I did wrong? Thank you very much in advance.

Asked By: Shuti

||

Answers:

The class BlobServiceClient in the python package does not have the method ls_files. For more details, please refer to here. we need to implement it by ourselves. In the document, you refer to, the user also does that.

Besides, according to my understanding, we want to list all the names of the blobs in one storage container. If so, please refer to the following code

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

conn_str = ''
blob_service_client = BlobServiceClient.from_connection_string(conn_str)

blob_list = blob_service_client.get_container_client('<container name>').list_blobs()

for blob in blob_list:
    print(blob.name)

enter image description here

Answered By: Jim Xu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.