在Python中,我将图像转换为字节。然后,像调用Azure认知服务时一样,将字节传递给Azure HTTP触发器功能应用程序终结点URL(Azure Portal)。
image_path = r"C:\Users\User\Desktop\bicycle.jpg"
image_data = open(image_path, "rb").read()
print(len(image_data)) # print length to compare later
url = "https://xxxx.azurewebsites.net/api/HTTPTrigger1........."
headers = {'Content-Type': 'application/octet-stream'}
response = requests.post(url, headers=headers,
data=image_data)
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何在Azure Portal上的函数应用程序中检索字节数据。我尝试了以下(C#),但没有成功。似乎ReadToEndAsync()不是要从请求正文中读取字节数据?还是因为HttpRequest?
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
byte[] imageBytes = Encoding.ASCII.GetBytes(requestBody);
log.LogInformation(imageBytes.Length.ToString());
// the length logged is totally not the same with len(image_data) in Python
//ignore the following lines (not related)
return name != null …Run Code Online (Sandbox Code Playgroud)