0

I'm developing an API using Azure Function Apps. The API works fine locally (using localhost). However, after publishing to Function App, I'm getting this error:

[Errno 30] Read-only file system

This error happens after I made the connection as a function to allow establishing new connection every time the API is requested. The data is taken from Azure Blob Storage container. The code:

DBConnection.py:

import os, uuid
from azure.storage.blob import BlockBlobService, AppendBlobService
from datetime import datetime
import pandas as pd
import dask.dataframe as dd
import logging

def BlobConnection() :

    try:
        print("Connecting...")
        #Establish connection

        container_name = 'somecontainer'
        blob_name = 'some_name.csv'
        file_path = 'somepath'
        account_name = 'XXXXXX'
        account_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

        blobService = BlockBlobService(account_name=account_name, account_key=account_key)


        blobService.get_blob_to_path(container_name, blob_name, file_path)

        df = dd.read_csv(file_path, dtype={'Bearing': 'int64', 'Speed': 'int64'})
        df = df.compute()
        
        return df

    except Exception as ex:
        print('Unable to connect!')
        print('Exception:')
        print(ex)
xixi
  • 59
  • 8

1 Answers1

0

You are probably running in Package or Zip. If so when you run your code the following line is trying to save the blob and can't. If you update that to use get_blob_to_bytes or get_blob_to_stream you would be fine.

blobService.get_blob_to_path(container_name, blob_name, file_path)

From [https://stackoverflow.com/questions/53630773/how-to-disable-read-only-mode-in-azure-function-app] Part 1 - Disabling read-only mode

You'll likely find if you're using the latest tools that your function app is in run-from-package mode, which means it's reading the files directly from the uploaded ZIP and so there's no way to edit it. You can turn that off by deleting the WEBSITE_RUN_FROM_ZIP or WEBSITE_RUN_FROM_PACKAGE application setting in the portal. Note this will clear your function app until the next time you publish.

If your tools are a little older, or if you've deployed using the latest tools but with func azure functionapp publish my-app-name --nozip then you can use the App Service Editor in Platform Features in the portal to edit the function.json files and remove the "generatedBy" setting, which will stop them being read-only.

Ron
  • 421
  • 3
  • 9
  • Hi, None of WEBSITE_RUN_FROM_ZIP or WEBSITE_RUN_FROM_PACKAGE is in the application setting. I added FUNCTION_APP_EDIT_MODE to readwrite. Seems not working too – xixi Jul 28 '20 at 01:44
  • Presuming you need to have the local file. can you try tempfile https://docs.python.org/3/library/tempfile.html There should be a temp location and then you can use tempfile.gettempdir() and store the file there. – Ron Jul 28 '20 at 01:47
  • Ok Ill give this a try. – xixi Jul 28 '20 at 01:49
  • Sorry I was looking at a few things. tempfile.NamedTemporaryFile looks like a good way to get the info https://stackoverflow.com/questions/51110783/is-it-possible-to-get-the-path-of-a-tempfile-in-python-3 with tempfile.NamedTemporaryFile(suffix='.csv', prefix=os.path.basename(__file__)) as tf: tf_directory = os.path.dirname(tf.name) – Ron Jul 28 '20 at 01:56
  • have you checked out this as well? https://github.com/manish/dask-azureblobfs Looks like you might be able to read directly from the blob. – Ron Jul 28 '20 at 02:02
  • I changed to get_blob_to_bytes or get_blob_to_stream, they dont work. I will try dask-azureblobfs. not sure how this will be relevant haha – xixi Jul 28 '20 at 02:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218737/discussion-between-xixi-and-ron). – xixi Jul 28 '20 at 07:54