Reading local files from Azure Static Web Page

Anton Delta 21 Reputation points
2021-03-03T11:34:19.247+00:00

I am trying out static web pages service from Azure. But I need to access a list of files (for example images stored on the server) from the client side. I am trying to implement a Function that will iterate over the files in a particular folder and return a list of names.

The problem is that I simply cannot access those files. It can't find them from the context in which the Function runs as if they are stored on another machine.

I now use this function to print the folders and files accessible to the python.

This is the function I used called GetResources:

import logging
import os
import sys
import azure.functions as func
import json

import os

def showFolderTree(path):
    show_files=True
    indentation=1
    file_output=False

    tree = []
    result=""
    if not show_files:
        for root, dirs, files in os.walk(path):
            level = root.replace(path, '').count(os.sep)
            indent = ' '*indentation*(level)
            tree.append('{}{}/'.format(indent,os.path.basename(root)))

    if show_files:
        for root, dirs, files in os.walk(path):
            level = root.replace(path, '').count(os.sep)
            indent = ' '*indentation*(level)
            tree.append('{}{}/'.format(indent,os.path.basename(root)))    
            for f in files:
                subindent=' ' * indentation * (level+1)
                tree.append('{}{}'.format(subindent,f))

    for line in tree:
        result+=line+"\n"
    return result

def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    # logging.info('Python HTTP trigger function processed a request.')  --> where is this logged?
    try:
        errors=context.function_directory+"\n"
    except Exception as e:
        error="context error\n"

    try:
        errors+=os.path.dirname(os.path.realpath(__file__))+"\n"
        errors+=os.getcwd()+"\n"

        errors+=showFolderTree("/")
    except Exception as e:
        errors+=e

    return func.HttpResponse(errors,status_code=200)

This function returns:

/home/site/wwwroot/GetResources
/home/site/wwwroot/GetResources
/home/site/wwwroot
/
app/
 .bash_logout
 .bashrc
 .profile
site/
wwwroot/
 .funcignore
 requirements.txt
 proxies.json
 .gitignore
 host.json
GetResources/
 function.json
 sample.dat
 __init__.py
__pycache__/
 __init__.cpython-38.pyc
.python_packages/
lib/
site-packages/
azure/
functions/
... many many other files

but I cannot find my files in the list. The wwwroot folder seems to match up with my local api folder.

What am I doing wrong?

Note: I have a student subscription

Side issue: I cannot find anywhere the logs generated by the Function (generated by the logging function).

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,249 questions
Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
761 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,516 Reputation points Microsoft Employee
    2021-03-04T05:55:48.837+00:00

    @Anton Delta While Azure Static Web Apps act as a proxy to Azure Functions for APIs, the Functions themself are hosted separate to the Static Web App and don't share the filesystem.

    So, instead of having these files pushed alongside the Web App, they should be stored in Blob Storage and from Functions, you could leverage Blob Storage Bindings to read/write them. To list files in a container, you need to bind to BlobContainerClient and use a method like BlobContainerClient.GetBlobs.

    0 comments No comments

0 additional answers

Sort by: Most helpful