skip to Main Content

I am unable to download a csv file from the storage container, here is my code as i keep getting an error… :


    import datetime
    import logging
    from azure.storage.blob import BlobServiceClient
    import azure.functions as func
    import os


    def main(mytimer: func.TimerRequest) -> None:
        utc_timestamp = datetime.datetime.utcnow().replace(
            tzinfo=datetime.timezone.utc).isoformat()

        if mytimer.past_due:
            logging.info('The timer is past due!')

        logging.info('Python timer trigger function ran at %s', utc_timestamp)

        # Azure Blob Storage credentials
        account_name = ''
        account_key = ''
        container_name = ''
        connection_string = f""

        # Connect to Blob Storage
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        container_client = blob_service_client.get_container_client(container_name)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob="blob.csv")
        filepath = "/home/site/wwwroot/TimerTriggerTEST"
        with open(file=os.path.join(filepath, 'filename'), mode="wb") as sample_blob:
            download_stream = blob_client.download_blob()
            sample_blob.write(download_stream.readall())

And here is a screenshot of the logs i get:

Logs

I have been getting this error even after trying a lot of things,

I tried to add in the Application settings FUNCTION_APP_EDIT_MODE with the value readwrite

I checked the documentation on how to download a blob and it didnt help,

I also saw that Only the "/tmp dir" is write-able on a few forums, but how am i supposed use and transfer file data? because sure i can download it to the tmp directory but i cannot copy paste the data to a csv file that i have in my home directory because i also get this error:
"Result: Failure Exception: OSError: [Errno 30] Read-only file system: ‘/home/site/wwwroot/TimerTrigger1/outbound.csv’ Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 479, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 752, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/TimerTrigger1/init.py", line 28, in main with open(‘/home/site/wwwroot/TimerTrigger1/outbound.csv’, ‘w’) as dest_file:"

I would need a solution to be able to do that if possible

2

Answers


  1. Chosen as BEST ANSWER

    After researching and troubleshooting i figured the best i could do is use blob containers to store information,

    Basically the idea is you process everything you need in the tmp file then either upload or download to the storage account, this makes it more tedious to code but is the only thing i found that works


  2. Thanks @zixer, below are my observations.

    I created storage account and container in azure portal.

    enter image description here

    I uploaded one file in container.

    enter image description here

    I made some changes in your code and the below code is works fine.

    import os
    import tempfile
    from azure.storage.blob import BlobServiceClient
    import azure.functions as func
    
    
    def main(mytimer: func.TimerRequest) -> None:
    
        account_name = 'welcome092'
        account_key = '**your key**'
        container_name = 'well'
        connection_string = "**your connection string**"
    
       
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        container_client = blob_service_client.get_container_client(container_name)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob="blob.csv")
    
      
        with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
            tmp_file_path = tmp_file.name
    
    
            with open(tmp_file_path, 'wb') as download_file:
                download_stream = blob_client.download_blob()
                download_file.write(download_stream.readall())
    
        destination_file_path = "C:/Users/v-ppesala/Downloads/pavanblob.csv"
    
        os.rename(tmp_file_path, destination_file_path)
    
    

    The above code runs successfully, and file downloaded in a specified path.

    enter image description here

    Output:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search