/
Function.cs
79 lines (68 loc) · 2.7 KB
/
Function.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START functions_http_form_data]
using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace HttpFormData;
public class Function : IHttpFunction
{
private readonly ILogger _logger;
public Function(ILogger<Function> logger) =>
_logger = logger;
public async Task HandleAsync(HttpContext context)
{
HttpResponse response = context.Response;
HttpRequest request = context.Request;
if (request.Method != "POST")
{
response.StatusCode = (int) HttpStatusCode.MethodNotAllowed;
return;
}
// This code will process each file uploaded.
string tempDirectory = Path.GetTempPath();
foreach (IFormFile file in request.Form.Files)
{
if (string.IsNullOrEmpty(file.FileName))
{
continue;
}
_logger.LogInformation("Processed file: {file}", file.FileName);
// Note: GCF's temp directory is an in-memory file system
// Thus, any files in it must fit in the instance's memory.
string outputPath = Path.Combine(tempDirectory, file.FileName);
// Note: files saved to a GCF instance itself may not persist across executions.
// Persistent files should be stored elsewhere, e.g. a Cloud Storage bucket.
using (FileStream output = File.Create(outputPath))
{
await file.CopyToAsync(output);
}
// TODO(developer): process saved files here
File.Delete(outputPath);
}
// This code will process other form fields.
foreach (KeyValuePair<string, StringValues> parameter in request.Form)
{
// TODO(developer): process field values here
_logger.LogInformation("Processed field '{key}' (value: '{value}')",
parameter.Key, (string) parameter.Value);
}
}
}
// [END functions_http_form_data]