forked from waiholiu/Pdf2ImageFunctionApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_app.py
80 lines (68 loc) · 2.92 KB
/
function_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import azure.functions as func
import logging
from azure.storage.blob import BlobServiceClient, ContainerClient
from process import process_file
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
@app.route(route="ProcessPDF")
def ProcessPDF(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Extract parameters from query
url = req.params.get('url')
CONNECTION_STRING = req.params.get('connectionString')
CONTAINER_NAME = req.params.get('containerName')
# Optionally, handle case where parameters might be in the request body
try:
req_body = req.get_json()
except ValueError:
req_body = {}
url = url or req_body.get('url')
CONNECTION_STRING = CONNECTION_STRING or req_body.get('connectionString')
CONTAINER_NAME = CONTAINER_NAME or req_body.get('containerName')
# Validate required parameters
if not all([url, CONNECTION_STRING, CONTAINER_NAME]):
return func.HttpResponse(
"Missing required parameters: url, connectionString, containerName",
status_code=400
)
# Ensure connection string is a string
if isinstance(CONNECTION_STRING, dict):
CONNECTION_STRING = CONNECTION_STRING.get('WebUrl', '')
# Validate that CONNECTION_STRING is now a string
if not isinstance(CONNECTION_STRING, str):
return func.HttpResponse(
"Invalid connectionString parameter format.",
status_code=400
)
# Convert CONTAINER_NAME to lowercase
CONTAINER_NAME = CONTAINER_NAME.lower()
# Create the container in Blob Storage if it doesn't exist
try:
blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING)
container_client = blob_service_client.get_container_client(CONTAINER_NAME)
if not container_client.exists():
container_client.create_container()
logging.info(f"Container '{CONTAINER_NAME}' created.")
else:
logging.info(f"Container '{CONTAINER_NAME}' already exists.")
except Exception as e:
logging.error(f"Error creating container: {e}")
return func.HttpResponse(
f"Error creating container: {e}",
status_code=500
)
try:
# Attempt to process the file
result = process_file(url, CONTAINER_NAME, CONNECTION_STRING)
logging.info(f"Processing result: {result}")
return func.HttpResponse(
"This HTTP triggered function executed successfully.",
status_code=200
)
except Exception as e:
logging.error(f"Error processing file: {e}")
# Additional logging for debugging
logging.error(f"URL: {url}, Container: {CONTAINER_NAME}, Connection String: {CONNECTION_STRING}")
return func.HttpResponse(
f"An error occurred processing your request: {e}",
status_code=500
)