question

AntonDelta-4082 avatar image
0 Votes"
AntonDelta-4082 asked ·

Reading local files from Azure Static Web Page



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-functionsazure-static-web-apps
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
0 Votes"
PramodValavala-MSFT answered ·

@AntonDelta-4082 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.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.