Skip to content

Commit 612f453

Browse files
committed
Test AF integration
1 parent 7451a7e commit 612f453

File tree

4 files changed

+34
-34
lines changed

4 files changed

+34
-34
lines changed

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.AppHost/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var storage = builder.AddAzureStorage("storage").RunAsEmulator();
44
var queue = storage.AddQueues("queue");
55
var blob = storage.AddBlobs("blob");
6+
var myBlobContainer = blob.AddBlobContainer("myblobcontainer");
7+
68
var eventHub = builder.AddAzureEventHubs("eventhubs")
79
.RunAsEmulator()
810
.AddHub("myhub");
@@ -20,6 +22,7 @@
2022
var funcApp = builder.AddAzureFunctionsProject<Projects.AzureFunctionsEndToEnd_Functions>("funcapp")
2123
.WithExternalHttpEndpoints()
2224
.WithReference(eventHub).WaitFor(eventHub)
25+
.WithReference(myBlobContainer).WaitFor(myBlobContainer)
2326
#if !SKIP_UNSTABLE_EMULATORS
2427
.WithReference(serviceBus).WaitFor(serviceBus)
2528
.WithReference(cosmosDb).WaitFor(cosmosDb)

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.Functions/MyAzureBlobTrigger.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
using Azure.Storage.Blobs;
12
using Microsoft.Azure.Functions.Worker;
23
using Microsoft.Extensions.Logging;
34

45
namespace AzureFunctionsEndToEnd.Functions;
56

6-
public class MyAzureBlobTrigger(ILogger<MyAzureBlobTrigger> logger)
7+
public class MyAzureBlobTrigger(ILogger<MyAzureBlobTrigger> logger, BlobContainerClient containerClient)
78
{
89
[Function(nameof(MyAzureBlobTrigger))]
910
[BlobOutput("test-files/{name}.txt", Connection = "blob")]
10-
public string Run([BlobTrigger("blobs/{name}", Connection = "blob")] string triggerString)
11+
public async Task<string> RunAsync([BlobTrigger("blobs/{name}", Connection = "blob")] string triggerString, FunctionContext context)
1112
{
12-
logger.LogInformation("C# blob trigger function invoked with {message}...", triggerString);
13+
var blobName = (string)context.BindingContext.BindingData["name"]!;
14+
await containerClient.UploadBlobAsync(blobName, new BinaryData(triggerString));
15+
16+
logger.LogInformation("C# blob trigger function invoked for 'blobs/{source}' with {message}...", blobName, triggerString);
1317
return triggerString.ToUpper();
1418
}
1519
}
16-

playground/AzureFunctionsEndToEnd/AzureFunctionsEndToEnd.Functions/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
builder.AddServiceDefaults();
77
builder.AddAzureQueueClient("queue");
88
builder.AddAzureBlobClient("blob");
9+
builder.AddAzureBlobContainerClient("myblobcontainer");
910
builder.AddAzureEventHubProducerClient("myhub");
1011
#if !SKIP_UNSTABLE_EMULATORS
1112
builder.AddAzureServiceBusClient("messaging");
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Data.Common;
5-
using System.Globalization;
6-
using System.Text;
4+
using System.Text.RegularExpressions;
75
using Aspire.Azure.Common;
86

97
namespace Aspire.Azure.Storage.Blobs;
108

119
/// <summary>
1210
/// Provides the client configuration settings for connecting to Azure Blob Storage container.
1311
/// </summary>
14-
public sealed class AzureBlobStorageContainerSettings : AzureStorageBlobsSettings, IConnectionStringSettings
12+
public sealed partial class AzureBlobStorageContainerSettings : AzureStorageBlobsSettings, IConnectionStringSettings
1513
{
14+
[GeneratedRegex(@"ContainerName=([^;]*);")]
15+
private static partial Regex ExtractContainerName();
16+
1617
public string? BlobContainerName { get; set; }
1718

1819
void IConnectionStringSettings.ParseConnectionString(string? connectionString)
@@ -22,38 +23,30 @@ void IConnectionStringSettings.ParseConnectionString(string? connectionString)
2223
return;
2324
}
2425

26+
// In the emulator mode, the connection string may look like:
27+
//
28+
// DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=...;BlobEndpoint=http://127.0.0.1:5555/devstoreaccount1;ContainerName=<container_name>;
29+
//
30+
// When run against the real Azure resources, the connection string will look similar to:
31+
//
32+
// https://<storage_name>.blob.core.windows.net/ContainerName=<container_name>;
33+
//
34+
// Retrieve the container name from the connection string, if it is present; and then
35+
// remove it as it will upset BlobServiceClient.
36+
37+
if (ExtractContainerName().Match(connectionString) is var match)
38+
{
39+
BlobContainerName = match.Groups[1].Value;
40+
connectionString = connectionString.Replace(match.Value, string.Empty);
41+
}
42+
2543
if (Uri.TryCreate(connectionString, UriKind.Absolute, out var uri))
2644
{
2745
ServiceUri = uri;
28-
29-
// TODO: how do we get the container name from the URI?
3046
}
3147
else
3248
{
33-
var connectionBuilder = new DbConnectionStringBuilder()
34-
{
35-
ConnectionString = connectionString
36-
};
37-
38-
if (connectionBuilder.TryGetValue("ContainerName", out var containerValue))
39-
{
40-
BlobContainerName = (string)containerValue;
41-
42-
// Remove it from the connection string, it is our custom property.
43-
connectionBuilder["ContainerName"] = null;
44-
}
45-
46-
// We can't use connectionBuilder.ConnectionString here, because connectionBuilder escapes values
47-
// adding quotes and other characters, which upsets the Azure SDK.
48-
// So, we have rebuilt the connection string manually.
49-
50-
StringBuilder builder = new();
51-
foreach (string keyword in connectionBuilder.Keys)
52-
{
53-
builder.Append(CultureInfo.InvariantCulture, $"{keyword}={connectionBuilder[keyword]};");
54-
}
55-
56-
ConnectionString = builder.ToString();
49+
ConnectionString = connectionString;
5750
}
5851
}
5952
}

0 commit comments

Comments
 (0)