1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . Data . Common ;
5
- using System . Globalization ;
6
- using System . Text ;
4
+ using System . Text . RegularExpressions ;
7
5
using Aspire . Azure . Common ;
8
6
9
7
namespace Aspire . Azure . Storage . Blobs ;
10
8
11
9
/// <summary>
12
10
/// Provides the client configuration settings for connecting to Azure Blob Storage container.
13
11
/// </summary>
14
- public sealed class AzureBlobStorageContainerSettings : AzureStorageBlobsSettings , IConnectionStringSettings
12
+ public sealed partial class AzureBlobStorageContainerSettings : AzureStorageBlobsSettings , IConnectionStringSettings
15
13
{
14
+ [ GeneratedRegex ( @"ContainerName=([^;]*);" ) ]
15
+ private static partial Regex ExtractContainerName ( ) ;
16
+
16
17
public string ? BlobContainerName { get ; set ; }
17
18
18
19
void IConnectionStringSettings . ParseConnectionString ( string ? connectionString )
@@ -22,38 +23,30 @@ void IConnectionStringSettings.ParseConnectionString(string? connectionString)
22
23
return ;
23
24
}
24
25
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
+
25
43
if ( Uri . TryCreate ( connectionString , UriKind . Absolute , out var uri ) )
26
44
{
27
45
ServiceUri = uri ;
28
-
29
- // TODO: how do we get the container name from the URI?
30
46
}
31
47
else
32
48
{
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 ;
57
50
}
58
51
}
59
52
}
0 commit comments