0

I would like to read csv file from Azure blob storage with python Azure function. Using the code below, I get the error:

[Errno 30] Read-only file system: '/home/site/wwwroot/my_csv_file'

And the code snippet is:

work_directory= os.getcwd()
filename = my_csv_file
account_name = <blob_storage>
account_key = <blob_key>

os.chmod(work_directory, 0o777)

input_fpath=os.path.join(work_directory, filename)
block_blob_service = BlockBlobService(account_name, account_key)
block_blob_service.get_blob_to_path(container_name=<input_container_name>, blob_name=filename,
                                          file_path=input_fpath)

How could I change permissions or how I can read csv to python in other way?

Mirit
  • 33
  • 3
  • 9

2 Answers2

1

SOLVED using get_blob_to_text instead of get_blob_to_path:

blobstring = block_blob_service.get_blob_to_text(<input_container_name>, file_name).content

The solution was found here.

chmod is not required. So, the whole code is the following:

filename = my_csv_file
account_name = <blob_storage>
account_key = <blob_key>

block_blob_service = BlockBlobService(account_name, account_key)
blobstring = block_blob_service.get_blob_to_text(<input_container_name>, filename).content
Mirit
  • 33
  • 3
  • 9
0

For now the Python Azure function doesn't allow to write the file, it's read-only mode and this is not able to change. So you could neither use chmod method nor use get_blob_to_path cause you could write file to your disk.

So maybe you could read your file to a stream then send it to the response. You could refer to my code, I use the Blob binding to read a text file.

def main(req: func.HttpRequest,inputblob: func.InputStream) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

name = req.params.get('name')
if not name:
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        name = req_body.get('name')

if name:
    return func.HttpResponse(inputblob.read(size=-1))
else:
    return func.HttpResponse(
         "Please pass a name on the query string or in the request body",
         status_code=400
    )

enter image description here

George Chen
  • 13,703
  • 2
  • 11
  • 26