[英]Save URL params as CSV file with Python and Azure Function
我想用 HTTP POST 發送一些數據,如下所示:
https://httptrigger-testfunction.azurewebsites.net/api/HttpTrigger1?id
=test&serial_id
=1254&device_tra
=302&received_time
=2021-03-01
我從此處的 Microsoft 示例中寫了一個 Azure function,從 HTTP POST 中讀取“名稱”。 現在,我想讀取上述數據並將其保存到 Blob 存儲上的 CSV 文件中。 我應該使用哪個模塊?
示例代碼:
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> 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(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
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
)
請參考我的代碼:
import logging
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import azure.functions as func
import os, uuid
import tempfile
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
connect_str = "<your-connection-string>"
container_name = "<your-container-name>"
id = req.params.get('id')
if not id:
try:
req_body = req.get_json()
except ValueError:
pass
else:
id = req_body.get('id')
serial_id = req.params.get('serial_id')
if not serial_id:
try:
req_body = req.get_json()
except ValueError:
pass
else:
serial_id = req_body.get('serial_id')
device_tra = req.params.get('device_tra')
if not device_tra:
try:
req_body = req.get_json()
except ValueError:
pass
else:
device_tra = req_body.get('device_tra')
received_time = req.params.get('received_time')
if not received_time:
try:
req_body = req.get_json()
except ValueError:
pass
else:
received_time = req_body.get('received_time')
# Create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Create the container
container_client = blob_service_client.get_container_client(container_name)
# Create a local directory to hold blob data
local_path = tempfile.gettempdir()
# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".csv"
upload_file_path = os.path.join(local_path, local_file_name)
logging.info(upload_file_path)
# Write text to the file
file = open(upload_file_path, 'w')
csv_content = id + "," + serial_id + "," + device_tra + "," + received_time
logging.info(csv_content)
file.write(csv_content)
file.close()
# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)
# Upload the created file
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)
if id:
return func.HttpResponse(f"Hello, {id}. This HTTP triggered function executed successfully.")
else:
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
)
或者您可以使用以下代碼:
import logging
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import azure.functions as func
import os, uuid
import tempfile
import csv
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
connect_str = "<your-connection-string>"
container_name = "<your-container-name>"
id = req.params.get('id')
if not id:
try:
req_body = req.get_json()
except ValueError:
pass
else:
id = req_body.get('id')
serial_id = req.params.get('serial_id')
if not serial_id:
try:
req_body = req.get_json()
except ValueError:
pass
else:
serial_id = req_body.get('serial_id')
device_tra = req.params.get('device_tra')
if not device_tra:
try:
req_body = req.get_json()
except ValueError:
pass
else:
device_tra = req_body.get('device_tra')
received_time = req.params.get('received_time')
if not received_time:
try:
req_body = req.get_json()
except ValueError:
pass
else:
received_time = req_body.get('received_time')
# Create the BlobServiceClient object which will be used to create a container client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Create the container
container_client = blob_service_client.get_container_client(container_name)
# Create a local directory to hold blob data
local_path = tempfile.gettempdir()
# Create a file in the local data directory to upload and download
local_file_name = str(uuid.uuid4()) + ".csv"
upload_file_path = os.path.join(local_path, local_file_name)
logging.info(upload_file_path)
with open(upload_file_path, 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
filewriter.writerow(['id', 'serial_id', 'device_tra', 'received_time'])
filewriter.writerow([id, serial_id, device_tra, received_time])
# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)
# Upload the created file
with open(upload_file_path, "rb") as data:
blob_client.upload_blob(data)
if id:
return func.HttpResponse(f"Hello, {id}. This HTTP triggered function executed successfully.")
else:
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
)
您可以使用存儲帳戶 Python SDK 。 這個適用於 ADLS Gen 2,如果您使用的是 Gen 1 或更早版本,請在此處找到正確的 SDK 。
查看“ 上傳文件”部分,它展示了如何將字符串數據寫入 blob。 例如,在下面的代碼中,您希望將 csv 內容放入 variable data
中。
from azure.storage.filedatalake import DataLakeFileClient
data = b"abc"
file = DataLakeFileClient.from_connection_string("my_connection_string",
file_system_name="myfilesystem", file_path="myfile")
file.append_data(data, offset=0, length=len(data))
file.flush_data(len(data))
您需要使用一些csv 寫入器寫入 memory 字符串(可能是StringIO
)而不是本地文件,然后將該字符串寫入 ADLS。
如果從代碼和示例中不明顯,您可以使用req.params
來訪問參數。 除了 body 沒有其他東西有getter
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.