gpt4 book ai didi

python - 使用 python 获取图像并将其上传到 blob

转载 作者:行者123 更新时间:2023-12-03 03:22:45 25 4
gpt4 key购买 nike

请帮我解决这个问题,我需要以某种方式捕获图像或视频并上传到 blob 中。在这种情况下,图像将在前端页面中收费。

我已经创建了一个代码来上传到 blob,但我不知道如何使用它。

import azure.functions as func
import os
from azure.storage.blob import BlobServiceClient


def main(req: func.HttpRequest) -> func.HttpResponse:
if req.method == "POST" and req.route_params.get('action') == 'uploadFiles':
try:
req_body = req.get_json()
file_path = req_body.get('file_path')
blob_name = req_body.get('blob_name')

if file_path and blob_name:
if os.path.exists(file_path):
# Upload do arquivo para o Blob Storage
with open(file_path, "rb") as file:
blob_client = container_client.get_blob_client(blob_name)
blob_client.upload_blob(file)
return func.HttpResponse(f"Arquivo {blob_name} enviado com sucesso.", status_code=200)
else:
return func.HttpResponse("O arquivo não existe.", status_code=400)
else:
return func.HttpResponse("Informe o caminho do arquivo e o nome do Blob.", status_code=400)
except ValueError:
return func.HttpResponse("O corpo da solicitação não está em formato JSON válido.", status_code=400)

我尝试使用flask,但是不起作用,我想知道是否有更简单的方法,也许使用azure api。

最佳答案

我对您的代码进行了一些更改,图像 blob 已成功上传到我的存储帐户容器。

代码:

import cv2
import os
import uuid
import azure.functions as func
from azure.storage.blob import BlobServiceClient

def capture_image():
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
if ret:
file_name = str(uuid.uuid4()) + '.jpg'
cv2.imwrite(file_name, frame)
return file_name
else:
return None

def upload_to_blob(file_path, blob_name):
connection_string = "<storage-conne-string>"
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
container_client = blob_service_client.get_container_client("<container-name>")
with open(file_path, "rb") as file:
blob_client = container_client.get_blob_client(blob_name)
blob_client.upload_blob(file)

def main(req: func.HttpRequest) -> func.HttpResponse:
if req.method == "POST" and req.params.get('action') == 'uploadFiles':
try:
req_body = req.get_json()
file_path = req_body.get('file_path')
blob_name = req_body.get('blob_name')

if file_path and blob_name:
upload_to_blob(file_path, blob_name)
os.remove(file_path)

return func.HttpResponse(f"File {blob_name} uploaded successfully.", status_code=200)
else:
return func.HttpResponse("Please provide the file path and blob name.", status_code=400)
except ValueError:
return func.HttpResponse("The request body is not in valid JSON format.", status_code=400)
else:
return func.HttpResponse("Unsupported operation.", status_code=405)

reuirement.txt:

azure-functions
opencv-python
azure-storage-blob

local.settings.json:

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<storage-conne-string>",
"FUNCTIONS_WORKER_RUNTIME": "python"
}
}

funxtion.json:

{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

运行上面的HTTP触发函数,如下所示,

单击fn + f5或单击运行>调试

输出:

它已成功运行,工作进程启动如下,

enter image description here

我的本​​地路径中有图像,如下所示,

enter image description here

然后,我在 Postman 中使用输出 URL 设置 Post 请求,并获得blob 上传成功到我的存储帐户容器,如下所示,

enter image description here

它在 VS Code 中给出了成功的输出,如下所示,

enter image description here

我在 Azure 门户的存储帐户容器中获得了图像 blob,如下所示,

enter image description here

现在,我的本地路径中没有图像,如下所示,

enter image description here

关于python - 使用 python 获取图像并将其上传到 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76517864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com