2

I got an error after running sometime my azure function, which is used to read the azure blob storage.

Error is

      ID 0dad768d-36d4-4c1a-85ae-2a5122533b3c
fail: Function.processor.User[0]
      Traceback (most recent call last):
        File "/usr/local/lib/python3.8/site-packages/azure/storage/blob/_download.py", line 360, in _initial_request
          location_mode, response = self._clients.blob.download(
        File "/usr/local/lib/python3.8/site-packages/azure/storage/blob/_generated/operations/_blob_operations.py", line 186, in download
          map_error(status_code=response.status_code, response=response, error_map=error_map)
        File "/usr/local/lib/python3.8/site-packages/azure/core/exceptions.py", line 102, in map_error
          raise error
      azure.core.exceptions.ResourceNotFoundError: Operation returned an invalid status 'The specified blob does not exist.'
  

My python code to access the file is

from azure.storage.filedatalake import DataLakeFileClient  

def get_file(self, file_path: str) -> Union[str, bytes, bytearray]:
    """Retrieve the file content of a file stored in the Data Lake
    
    Args:
        file_path (str): The path to the file
    
    Returns:
        Union[str, bytes, bytearray]: File content
    
    Raises:
        Exception: Description
    """

    try:
        file = DataLakeFileClient(
            account_url=self.account_url,
            credential=self.account_key,
            file_system_name=self.fs_name,
            file_path=file_path)
        return bytes(file.download_file().readall())
    except ResourceNotFoundError as e:
        raise Exception("No such file")

Anybody know what it is the solution

BLOB

screen shot


 datalake is blob

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
galiylama
  • 45
  • 2
  • 7

1 Answers1

0

enter image description here

Sample code(please make sure 'myfile' exists.):

from azure.storage.filedatalake import DataLakeServiceClient 
connect_str = "DefaultEndpointsProtocol=https;AccountName=0730bowmanwindow;AccountKey=xxxxxx;EndpointSuffix=core.windows.net"
datalake_service_client = DataLakeServiceClient.from_connection_string(connect_str)
myfilesystem = "test"
myfolder     = "test"
myfile       = "FileName.txt"

file_system_client = datalake_service_client.get_file_system_client(myfilesystem)            
directory_client = file_system_client.create_directory(myfolder)         
directory_client = file_system_client.get_directory_client(myfolder)

file_client = directory_client.get_file_client(myfile)
print(file_client.download_file().readall())
Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • @galiylama You can try to see if there are errors based on this. – Cindy Pau Mar 26 '21 at 08:01
  • file_system_client.create_directory(myfolder) is it necessary.folder already there? – galiylama Mar 26 '21 at 10:31
  • in this method datalake_service_client = DataLakeServiceClient.from_connection_string(connect_str) filepath and filesystem should define – galiylama Mar 26 '21 at 12:12
  • @galiylama create_directory should be no need to create folder manually. – Cindy Pau Mar 26 '21 at 12:15
  • @galiylama from_connection_string is create a link to datalake. The next two steps is define the filesystem and folder and blobname. – Cindy Pau Mar 26 '21 at 12:17
  • got error datalake_service_client object has no attribute 'get_file_system_client'. – galiylama Mar 26 '21 at 12:50
  • @galiylama This is the [official api reference](https://learn.microsoft.com/en-us/python/api/azure-storage-file-datalake/azure.storage.filedatalake.datalakeserviceclient?view=azure-python). You can get everything from it and you will find get_file_system_client is in it.:) – Cindy Pau Mar 26 '21 at 12:58
  • @galiylama Still get `has no attribute 'get_file_system_client'.` ? – Cindy Pau Mar 30 '21 at 06:25
  • yes. no such extension found get_file_system_client – galiylama Mar 30 '21 at 07:45
  • i am accepting you answer. But still not working my solution – galiylama Mar 30 '21 at 07:46
  • @galiylama What the package you installed? – Cindy Pau Mar 30 '21 at 08:01
  • @galiylama This is official [datalake tutorials for python language](https://learn.microsoft.com/en-us/azure/storage/blobs/data-lake-storage-directory-file-acl-python), maybe you can create a virtual environment on local(such as conda or python virtual environment), reinstall package and try again. – Cindy Pau Mar 30 '21 at 08:08
  • in requirement file azure-storage-file-datalake – galiylama Mar 30 '21 at 09:25