Skip to content

Commit 2b78e7b

Browse files
[Samples] WorkerBindingSamples project with blob function examples (#1152)
Co-authored-by: Surgupta <[email protected]>
1 parent c435dda commit 2b78e7b

File tree

10 files changed

+297
-0
lines changed

10 files changed

+297
-0
lines changed

DotNetWorker.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ EndProject
117117
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Worker.Extensions.Tables", "extensions\Worker.Extensions.Tables\src\Worker.Extensions.Tables.csproj", "{004DEF24-7EBB-499D-BD1C-E940AC4E122D}"
118118
EndProject
119119
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Worker.Extensions.Shared", "extensions\Worker.Extensions.Shared\Worker.Extensions.Shared.csproj", "{277D77B9-8915-41E3-8763-0B66328ADDDA}"
120+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkerBindingSamples", "samples\WorkerBindingSamples\WorkerBindingSamples.csproj", "{901DA3C3-3ABA-4859-89D3-63343ED2A0AC}"
120121
EndProject
121122
Global
122123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -288,6 +289,10 @@ Global
288289
{277D77B9-8915-41E3-8763-0B66328ADDDA}.Debug|Any CPU.Build.0 = Debug|Any CPU
289290
{277D77B9-8915-41E3-8763-0B66328ADDDA}.Release|Any CPU.ActiveCfg = Release|Any CPU
290291
{277D77B9-8915-41E3-8763-0B66328ADDDA}.Release|Any CPU.Build.0 = Release|Any CPU
292+
{901DA3C3-3ABA-4859-89D3-63343ED2A0AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
293+
{901DA3C3-3ABA-4859-89D3-63343ED2A0AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
294+
{901DA3C3-3ABA-4859-89D3-63343ED2A0AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
295+
{901DA3C3-3ABA-4859-89D3-63343ED2A0AC}.Release|Any CPU.Build.0 = Release|Any CPU
291296
EndGlobalSection
292297
GlobalSection(SolutionProperties) = preSolution
293298
HideSolutionNode = FALSE
@@ -338,6 +343,7 @@ Global
338343
{65DE66B6-568F-46AC-8F0D-C79A02F48214} = {083592CA-7DAB-44CE-8979-44FAFA46AEC3}
339344
{004DEF24-7EBB-499D-BD1C-E940AC4E122D} = {A7B4FF1E-3DF7-4F28-9333-D0961CDDF702}
340345
{277D77B9-8915-41E3-8763-0B66328ADDDA} = {A7B4FF1E-3DF7-4F28-9333-D0961CDDF702}
346+
{901DA3C3-3ABA-4859-89D3-63343ED2A0AC} = {9D6603BD-7EA2-4D11-A69C-0D9E01317FD6}
341347
EndGlobalSection
342348
GlobalSection(ExtensibilityGlobals) = postSolution
343349
SolutionGuid = {497D2ED4-A13E-4BCA-8D29-F30CA7D0EA4A}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.IO;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Azure.Storage.Blobs;
9+
using Microsoft.Azure.Functions.Worker;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace SampleApp
13+
{
14+
public class BlobTriggerBindingSamples
15+
{
16+
private readonly ILogger<BlobTriggerBindingSamples> _logger;
17+
18+
public BlobTriggerBindingSamples(ILogger<BlobTriggerBindingSamples> logger)
19+
{
20+
_logger = logger;
21+
}
22+
23+
[Function(nameof(BlobClientFunction))]
24+
public async Task BlobClientFunction(
25+
[BlobTrigger("client-trigger/{name}")] BlobClient client, string name)
26+
{
27+
var downloadResult = await client.DownloadContentAsync();
28+
var content = downloadResult.Value.Content.ToString();
29+
_logger.LogInformation("Blob name: {name} -- Blob content: {content}", name, content);
30+
}
31+
32+
[Function(nameof(BlobStreamFunction))]
33+
public async Task BlobStreamFunction(
34+
[BlobTrigger("stream-trigger/{name}")] Stream stream, string name)
35+
{
36+
using var blobStreamReader = new StreamReader(stream);
37+
var content = await blobStreamReader.ReadToEndAsync();
38+
_logger.LogInformation("Blob name: {name} -- Blob content: {content}", name, content);
39+
}
40+
41+
[Function(nameof(BlobByteArrayFunction))]
42+
public void BlobByteArrayFunction(
43+
[BlobTrigger("byte-trigger")] Byte[] data)
44+
{
45+
_logger.LogInformation($"Blob content: {Encoding.Default.GetString(data)}");
46+
}
47+
48+
[Function(nameof(BlobStringFunction))]
49+
public void BlobStringFunction(
50+
[BlobTrigger("string-trigger")] string data)
51+
{
52+
_logger.LogInformation($"Blob content: {data}");
53+
}
54+
55+
[Function(nameof(BlobBookFunction))]
56+
public void BlobBookFunction(
57+
[BlobTrigger("book-trigger")] Book data)
58+
{
59+
_logger.LogInformation($"Id: {data.Id} - Name: {data.Name}");
60+
}
61+
}
62+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Microsoft.Azure.Functions.Worker;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace SampleApp
8+
{
9+
public class ExpressionFunction
10+
{
11+
private readonly ILogger<ExpressionFunction> _logger;
12+
13+
public ExpressionFunction(ILogger<ExpressionFunction> logger)
14+
{
15+
_logger = logger;
16+
}
17+
18+
[Function(nameof(ExpressionFunction))]
19+
public void Run(
20+
[QueueTrigger("expression-trigger")] Book book,
21+
[BlobInput("input-container/{id}.txt")] string myBlob,
22+
FunctionContext context)
23+
{
24+
_logger.LogInformation("Trigger content: {content}", book);
25+
_logger.LogInformation("Blob content: {content}", myBlob);
26+
}
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
namespace SampleApp
5+
{
6+
public class Book
7+
{
8+
public string Id { get; set; }
9+
public string Name { get; set; }
10+
}
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 Microsoft.Extensions.Hosting;
6+
7+
namespace SampleApp
8+
{
9+
public class Program
10+
{
11+
public static void Main()
12+
{
13+
var host = new HostBuilder()
14+
.ConfigureFunctionsWorkerDefaults()
15+
.Build();
16+
17+
host.Run();
18+
}
19+
}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Extensions Sample
2+
3+
This sample project provides simple examples of how to use various extensions supported by the .NET Worker.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net7.0</TargetFramework>
4+
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.12.0-preview1" />
11+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.9.0-preview1" />
12+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
13+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Blobs" Version="5.1.0-preview1" />
14+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage.Queues" Version="5.0.0" />
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
16+
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
17+
</ItemGroup>
18+
<ItemGroup>
19+
<None Update="host.json">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
22+
<None Update="local.settings.json">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
25+
</None>
26+
</ItemGroup>
27+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "2.0",
3+
"logging": {
4+
"applicationInsights": {
5+
"samplingSettings": {
6+
"isEnabled": true,
7+
"excludedTypes": "Request"
8+
}
9+
}
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
5+
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
6+
}
7+
}

0 commit comments

Comments
 (0)