python - 使用 azure blob 存储向 API 发出 http post 请求

标签 python python-3.x azure http-post azure-blob-storage

我正在尝试使用 Microsoft 的人脸 api 发出 http post 请求,以便将其与我的 azure blob 存储帐户中的照片连接起来。当我运行以下代码时,我收到多个错误,例如握手错误ssl例程类型错误。我很感激任何帮助!问题代码是:

api_response = requests.post(url, headers=headers, data=blob)

显然,这里的上下文是我之前运行的。第一个 block 设置存储帐户:

%matplotlib inline
import matplotlib.pyplot as plt
import io
from io import StringIO
import numpy as np
import cv2
from PIL import Image  
from PIL import Image
import os 
from array import array


azure_storage_account_name = 'musicsurveyphotostorage'
azure_storage_account_key = None  #dont need key... we will access public blob... 
if azure_storage_account_name is None:
raise Exception("You must provide a name for an Azure Storage account")   
from azure.storage.blob import BlockBlobService
blob_service = BlockBlobService(azure_storage_account_name, azure_storage_account_key)

# select container (folder) name where the files resides
container_name = 'musicsurveyphotostorage'

# list files in the selected folder
generator = blob_service.list_blobs(container_name)
blob_prefix = 'https://{0}.blob.core.windows.net/{1}/{2}'

# load image file to process
blob_name = 'shiba.jpg'  #name of image I have stored
blob = blob_service.get_blob_to_bytes(container_name, blob_name)
image_file_in_mem = io.BytesIO(blob.content)
img_bytes = Image.open(image_file_in_mem)

第二个 block 调用 API 和有问题的post 请求:

#CALL OUT THE API
import requests
import urllib

url_face_api = 'https://eastus.api.cognitive.microsoft.com/face/v1.0'
api_key ='____'

#WHICH PARAMETERS ATTRIBUTES DO YOU WANT RETURNED   
headers = {'Content-Type': 'application/octet-stream', 'Ocp-Apim- 
Subscription-Key':api_key}

params = urllib.parse.urlencode({
    'returnFaceId': 'true',
    'returnFaceLandmarks': 'true',
    'returnFaceAttributes': 'age,gender,smile,facialHair,headPose,glasses',
})

query_string = '?{0}'.format(params)
url = url_face_api  + query_string


#THIS IS THE PROBLEM CODE  
api_response = requests.post(url, headers=headers, data=blob)
#print out output in json
import json
res_json = json.loads(api_response.content.decode('utf-8'))
print(json.dumps(res_json, indent=2, sort_keys=True))

最佳答案

如果我打开 Fiddler,我也可以重现您提到的问题。如果是这种情况,您可以在发送请求期间暂停使用 fiddler 来捕获请求。

根据我的测试,您的代码中有 2 行代码需要更改。更多信息您可以引用屏幕截图。 我们还可以从 azure offical document 获取一些演示代码。

url_face_api = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect' # in your case miss detect
api_response = requests.post(url, headers=headers,data=blob.content) # data should be blob.content

enter image description here

关于python - 使用 azure blob 存储向 API 发出 http post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49743657/

相关文章:

python - Keras CNN,详细的训练进度条显示

python ftplib Transfercmd() 二进制模式

python - spacy 标记化合并了错误的标记

Azure 虚拟机 - Rest API 调用不起作用

python - 在 CentOS 上使用 Python 脚本连接 MySQL

Python理论——不使用len函数的列表比较

python - 如何在 python 中翻转一个字节?

git - 无法通过 Git 在 azure 上部署静态网站

azure - 在 Azure 上安装占用空间较小的 Windows Server 2008

python - "Driver is not defined"Python/ Selenium