|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Net; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Azure.Storage.Blobs; |
| 11 | +using Microsoft.Azure.Functions.Worker; |
| 12 | +using Microsoft.Azure.Functions.Worker.Http; |
| 13 | +using Microsoft.Extensions.Logging; |
| 14 | + |
| 15 | +namespace SampleApp |
| 16 | +{ |
| 17 | + public class BlobInputBindingSamples |
| 18 | + { |
| 19 | + private readonly ILogger<BlobInputBindingSamples> _logger; |
| 20 | + |
| 21 | + public BlobInputBindingSamples(ILogger<BlobInputBindingSamples> logger) |
| 22 | + { |
| 23 | + _logger = logger; |
| 24 | + } |
| 25 | + |
| 26 | + [Function(nameof(BlobInputClientFunction))] |
| 27 | + public async Task<HttpResponseData> BlobInputClientFunction( |
| 28 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 29 | + [BlobInput("input-container/sample1.txt")] BlobClient client) |
| 30 | + { |
| 31 | + var downloadResult = await client.DownloadContentAsync(); |
| 32 | + var content = downloadResult.Value.Content.ToString(); |
| 33 | + |
| 34 | + _logger.LogInformation("Blob content: {content}", content); |
| 35 | + |
| 36 | + return req.CreateResponse(HttpStatusCode.OK); |
| 37 | + } |
| 38 | + |
| 39 | + [Function(nameof(BlobInputStreamFunction))] |
| 40 | + public async Task<HttpResponseData> BlobInputStreamFunction( |
| 41 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 42 | + [BlobInput("input-container/sample1.txt")] Stream stream) |
| 43 | + { |
| 44 | + using var blobStreamReader = new StreamReader(stream); |
| 45 | + _logger.LogInformation("Blob content: {stream}", await blobStreamReader.ReadToEndAsync()); |
| 46 | + |
| 47 | + return req.CreateResponse(HttpStatusCode.OK); |
| 48 | + } |
| 49 | + |
| 50 | + [Function(nameof(BlobInputByteArrayFunction))] |
| 51 | + public HttpResponseData BlobInputByteArrayFunction( |
| 52 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 53 | + [BlobInput("input-container/sample1.txt")] Byte[] data) |
| 54 | + { |
| 55 | + _logger.LogInformation($"Blob content: {Encoding.Default.GetString(data)}"); |
| 56 | + return req.CreateResponse(HttpStatusCode.OK); |
| 57 | + } |
| 58 | + |
| 59 | + [Function(nameof(BlobInputStringFunction))] |
| 60 | + public HttpResponseData BlobInputStringFunction( |
| 61 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, string filename, |
| 62 | + [BlobInput("input-container/{filename}")] string data) |
| 63 | + { |
| 64 | + _logger.LogInformation($"Blob content: {data}"); |
| 65 | + return req.CreateResponse(HttpStatusCode.OK); |
| 66 | + } |
| 67 | + |
| 68 | + [Function(nameof(BlobInputBookFunction))] |
| 69 | + public HttpResponseData BlobInputBookFunction( |
| 70 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 71 | + [BlobInput("input-container/book.json")] Book data) |
| 72 | + { |
| 73 | + _logger.LogInformation($"Book name: {data.Name}"); |
| 74 | + return req.CreateResponse(HttpStatusCode.OK); |
| 75 | + } |
| 76 | + |
| 77 | + [Function(nameof(BlobInputCollectionFunction))] |
| 78 | + public HttpResponseData BlobInputCollectionFunction( |
| 79 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 80 | + [BlobInput("input-container", IsBatched = true)] IEnumerable<BlobClient> blobs) |
| 81 | + { |
| 82 | + _logger.LogInformation("Blobs within container:"); |
| 83 | + |
| 84 | + foreach (BlobClient blob in blobs) |
| 85 | + { |
| 86 | + _logger.LogInformation("Blob name: {blobName}, Container name: {containerName}", blob.Name, blob.BlobContainerName); |
| 87 | + } |
| 88 | + |
| 89 | + return req.CreateResponse(HttpStatusCode.OK); |
| 90 | + } |
| 91 | + |
| 92 | + [Function(nameof(BlobInputStringArrayFunction))] |
| 93 | + public HttpResponseData BlobInputStringArrayFunction( |
| 94 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 95 | + [BlobInput("input-container", IsBatched = true)] string[] blobContent) |
| 96 | + { |
| 97 | + _logger.LogInformation("Content of all blobs within container:"); |
| 98 | + |
| 99 | + foreach (var item in blobContent) |
| 100 | + { |
| 101 | + _logger.LogInformation(item); |
| 102 | + } |
| 103 | + |
| 104 | + return req.CreateResponse(HttpStatusCode.OK); |
| 105 | + } |
| 106 | + |
| 107 | + [Function(nameof(BlobInputBookArrayFunction))] |
| 108 | + public HttpResponseData BlobInputBookArrayFunction( |
| 109 | + [HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req, |
| 110 | + [BlobInput("input-container", IsBatched = true)] Book[] books) |
| 111 | + { |
| 112 | + _logger.LogInformation("Content of all blobs within container:"); |
| 113 | + |
| 114 | + foreach (var item in books) |
| 115 | + { |
| 116 | + _logger.LogInformation(item.Name); |
| 117 | + } |
| 118 | + |
| 119 | + return req.CreateResponse(HttpStatusCode.OK); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments