@Venkata Naga Sukumar Vinnakota
I was able to get this to work after some tweaks to your current code. Here's what I have that worked.
__init__.py
import logging
import os
import azure.functions as func
import azure.storage.blob as blob
import pandas as pd
import requests
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Get the data from the API
url = "https://api.themoviedb.org/3/trending/movie/day?language=en-US"
headers = {
"accept": "application/json",
"Authorization": f"Bearer {os.getenv('API_KEY')}"
}
response = requests.get(url, headers=headers)
results = response.json()["results"]
# Create a dataframe from the results
df = pd.DataFrame(results)
df_selected = df[["id", "title", "overview", "poster_path", "vote_average", "vote_count"]]
output = df_selected.to_csv(index=False)
logging.info(df_selected.head())
# Upload the csv to blob storage
# Create a blob client using the local file name as the name for the blob
try:
blob_service_client = blob.BlobServiceClient.from_connection_string(os.getenv('AZURE_STORAGE_CONNECTION_STRING'))
container_name = "movies"
blob_name = "trending.csv"
container_client = blob_service_client.get_container_client(container_name)
if(not container_client.exists()):
container_client.create_container()
blob_client = container_client.get_blob_client(blob=blob_name)
blob_client.upload_blob(output, blob_type="BlockBlob")
except Exception as ex:
logging.error('Exception:')
logging.error(ex)
return func.HttpResponse("Error occurred processing request", status_code=500)
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
I'm using the latest modules in my requirements.txt
# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues
azure-functions
azure-storage-blob
pandas
requests
Locally, I'm using 3.10 interpreter because for some reason, I couldn't get the imports to work correctly. Even though they were installed, when debugging the function app locally, it would also report a module load error. On the deployed function app, it's using python 3.8 runtime. I did also have an issue on the function app where for whatever reason, the function app didn't create container whereas locally, it did. I created the container manually and got the 200.
The main difference is I'm aliasing some of the imported modules, so try that and see if that resolves your issue.