Azure 功能无法正常工作?
Azure function not working properly ?
我有一个 azure 函数,它使用像这样的多种形式获取输入文件 -
public static class function
{
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log,string imagename)
{
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
log.Info("C# HTTP trigger function processed a request.");
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
如您所见,目前我没有对该文件进行任何操作。我想先得到它 运行。
我的 swagger 文件如下所示:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Testfunction"
},
"host": "Testfunction.azurewebsites.net",
"paths": {
"/api/Function1": {
"post": {
"tags": [
"Function1"
],
"operationId": "UploadImage",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"parameters": [
{
"name": "file",
"in": "formData",
"required": true,
"type": "file",
"x-ms-media-kind": "image"
},
{
"name": "fileName",
"in": "query",
"required": false,
"type": "string"
}
],
"responses": {
"200": {
"description": "Saved successfully",
"schema": {
"$ref": "#/definitions/UploadedFileInfo"
}
},
"400": {
"description": "Could not find file to upload"
}
},
"summary": "Image Upload",
"description": "Image Upload"
}
}},
"definitions": {
"UploadedFileInfo": {
"type": "object",
"properties": {
"FileName": {
"type": "string"
},
"FileExtension": {
"type": "string"
},
"FileURL": {
"type": "string"
},
"ContentType": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"AAD": {
"type": "oauth2",
"flow": "accessCode",
"authorizationUrl": "https://login.windows.net/common/oauth2/authorize",
"tokenUrl": "https://login.windows.net/common/oauth2/token",
"scopes": {}
}
},
"security": [
{
"AAD": []
}
],
"tags": []
}
我已经正确配置了 Auth0,但是当我在 Powerapps 中尝试 运行 时,它 returns 以未知错误作为响应。
我正在尝试将图像发送到我想将其转换为 PDF 的 azure 函数。我怎样才能做到这一点?
根据您的代码,您似乎正在使用 precompiled functions。我发现您的 HttpTrigger 函数代码与您提供的 swagger 文件不匹配。对于一个简单的方法,我只使用 Azure 门户并创建我的 HttpTrigger 函数如下:
run.csx:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage request, TraceWriter log)
{
//Check if the request contains multipart/form-data.
if (!request.Content.IsMimeMultipartContent())
{
return request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
//The root path where the content of MIME multipart body parts are written to
string root = Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), @"site\wwwroot\HttpTriggerCSharp1\");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data
await request.Content.ReadAsMultipartAsync(provider);
List<UploadedFileInfo> files = new List<UploadedFileInfo>();
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
var fileInfo = new FileInfo(file.Headers.ContentDisposition.FileName.Trim('"'));
files.Add(new UploadedFileInfo()
{
FileName = fileInfo.Name,
ContentType = file.Headers.ContentType.MediaType,
FileExtension = fileInfo.Extension,
FileURL = file.LocalFileName
});
}
return request.CreateResponse(HttpStatusCode.OK, files, "application/json");
}
public class UploadedFileInfo
{
public string FileName { get; set; }
public string FileExtension { get; set; }
public string FileURL { get; set; }
public string ContentType { get; set; }
}
注意: MIME 多部分正文部分存储在您的临时文件夹下。您可以通过 file.LocalFileName
检索临时文件的完整路径,然后使用相关库处理转换,然后保存 PDF 文件并删除临时文件,然后 return 转换文件到客户端。
测试:
此外,我建议您将文件存储在 Azure Blob Storage. And you could refer to this official toturial 下以便开始使用它。
我有一个 azure 函数,它使用像这样的多种形式获取输入文件 -
public static class function
{
[FunctionName("Function1")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log,string imagename)
{
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
// Set name to query string or body data
name = name ?? data?.name;
log.Info("C# HTTP trigger function processed a request.");
return name == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
: req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}
如您所见,目前我没有对该文件进行任何操作。我想先得到它 运行。
我的 swagger 文件如下所示:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Testfunction"
},
"host": "Testfunction.azurewebsites.net",
"paths": {
"/api/Function1": {
"post": {
"tags": [
"Function1"
],
"operationId": "UploadImage",
"consumes": [
"multipart/form-data"
],
"produces": [
"application/json",
"text/json",
"application/xml",
"text/xml"
],
"parameters": [
{
"name": "file",
"in": "formData",
"required": true,
"type": "file",
"x-ms-media-kind": "image"
},
{
"name": "fileName",
"in": "query",
"required": false,
"type": "string"
}
],
"responses": {
"200": {
"description": "Saved successfully",
"schema": {
"$ref": "#/definitions/UploadedFileInfo"
}
},
"400": {
"description": "Could not find file to upload"
}
},
"summary": "Image Upload",
"description": "Image Upload"
}
}},
"definitions": {
"UploadedFileInfo": {
"type": "object",
"properties": {
"FileName": {
"type": "string"
},
"FileExtension": {
"type": "string"
},
"FileURL": {
"type": "string"
},
"ContentType": {
"type": "string"
}
}
}
},
"securityDefinitions": {
"AAD": {
"type": "oauth2",
"flow": "accessCode",
"authorizationUrl": "https://login.windows.net/common/oauth2/authorize",
"tokenUrl": "https://login.windows.net/common/oauth2/token",
"scopes": {}
}
},
"security": [
{
"AAD": []
}
],
"tags": []
}
我已经正确配置了 Auth0,但是当我在 Powerapps 中尝试 运行 时,它 returns 以未知错误作为响应。
我正在尝试将图像发送到我想将其转换为 PDF 的 azure 函数。我怎样才能做到这一点?
根据您的代码,您似乎正在使用 precompiled functions。我发现您的 HttpTrigger 函数代码与您提供的 swagger 文件不匹配。对于一个简单的方法,我只使用 Azure 门户并创建我的 HttpTrigger 函数如下:
run.csx:
using System.Net;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage request, TraceWriter log)
{
//Check if the request contains multipart/form-data.
if (!request.Content.IsMimeMultipartContent())
{
return request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}
//The root path where the content of MIME multipart body parts are written to
string root = Path.Combine(System.Environment.GetEnvironmentVariable("HOME"), @"site\wwwroot\HttpTriggerCSharp1\");
var provider = new MultipartFormDataStreamProvider(root);
// Read the form data
await request.Content.ReadAsMultipartAsync(provider);
List<UploadedFileInfo> files = new List<UploadedFileInfo>();
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
var fileInfo = new FileInfo(file.Headers.ContentDisposition.FileName.Trim('"'));
files.Add(new UploadedFileInfo()
{
FileName = fileInfo.Name,
ContentType = file.Headers.ContentType.MediaType,
FileExtension = fileInfo.Extension,
FileURL = file.LocalFileName
});
}
return request.CreateResponse(HttpStatusCode.OK, files, "application/json");
}
public class UploadedFileInfo
{
public string FileName { get; set; }
public string FileExtension { get; set; }
public string FileURL { get; set; }
public string ContentType { get; set; }
}
注意: MIME 多部分正文部分存储在您的临时文件夹下。您可以通过 file.LocalFileName
检索临时文件的完整路径,然后使用相关库处理转换,然后保存 PDF 文件并删除临时文件,然后 return 转换文件到客户端。
测试:
此外,我建议您将文件存储在 Azure Blob Storage. And you could refer to this official toturial 下以便开始使用它。