|
| 1 | +using Azure.Storage.Blobs; |
| 2 | + |
| 3 | +namespace Umbraco.StorageProviders.AzureBlob.IO; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Extension methods for <see cref="AzureBlobFileSystemOptions" />. |
| 7 | +/// </summary> |
| 8 | +public static class AzureBlobFileSystemOptionsExtensions |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Creates a <see cref="BlobContainerClient" /> using the default constructor accepting a connection string and container name. |
| 12 | + /// </summary> |
| 13 | + /// <param name="options">The Azure Blob File System options.</param> |
| 14 | + /// <exception cref="System.ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
| 15 | + public static void CreateBlobContainerClientUsingDefault(this AzureBlobFileSystemOptions options) |
| 16 | + { |
| 17 | + ArgumentNullException.ThrowIfNull(options); |
| 18 | + |
| 19 | + options.BlobContainerClientFactory = AzureBlobFileSystemOptions.DefaultBlobContainerClientFactory; |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Creates a <see cref="BlobContainerClient" /> using the default constructor accepting a connection string, container name and Blob client options. |
| 24 | + /// </summary> |
| 25 | + /// <param name="options">The Azure Blob File System options.</param> |
| 26 | + /// <param name="blobClientOptions">The Azure Blob client options.</param> |
| 27 | + /// <exception cref="System.ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
| 28 | + public static void CreateBlobContainerClientUsingOptions(this AzureBlobFileSystemOptions options, BlobClientOptions blobClientOptions) |
| 29 | + { |
| 30 | + ArgumentNullException.ThrowIfNull(options); |
| 31 | + |
| 32 | + options.BlobContainerClientFactory = options => new BlobContainerClient(options.ConnectionString, options.ContainerName, blobClientOptions); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Parses the connection string to a URI and invokes the <paramref name="create" /> delegate to create a <see cref="BlobContainerClient" />. |
| 37 | + /// </summary> |
| 38 | + /// <param name="options">The Azure Blob File System options.</param> |
| 39 | + /// <param name="create">The delegate to create a <see cref="BlobContainerClient" /> from the parsed <see cref="Uri" />.</param> |
| 40 | + /// <remarks> |
| 41 | + /// If the connection string can't be parsed to a URI, the existing delegate will be used instead. |
| 42 | + /// </remarks> |
| 43 | + /// <exception cref="System.ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
| 44 | + public static void TryCreateBlobContainerClientUsingUri(this AzureBlobFileSystemOptions options, Func<Uri, BlobContainerClient> create) |
| 45 | + { |
| 46 | + ArgumentNullException.ThrowIfNull(options); |
| 47 | + |
| 48 | + Func<AzureBlobFileSystemOptions, BlobContainerClient> defaultCreate = options.BlobContainerClientFactory; |
| 49 | + options.BlobContainerClientFactory = options => |
| 50 | + { |
| 51 | + if (Uri.TryCreate(options.ConnectionString, UriKind.Absolute, out Uri? blobContainerUri)) |
| 52 | + { |
| 53 | + var blobUriBuilder = new BlobUriBuilder(blobContainerUri) |
| 54 | + { |
| 55 | + BlobContainerName = options.ContainerName |
| 56 | + }; |
| 57 | + |
| 58 | + return create(blobUriBuilder.ToUri()); |
| 59 | + } |
| 60 | + |
| 61 | + return defaultCreate(options); |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Creates the Azure Blob Container client using the configured factory. |
| 67 | + /// </summary> |
| 68 | + /// <param name="options">The Azure Blob File System options.</param> |
| 69 | + /// <returns> |
| 70 | + /// The Azure Blob Container client. |
| 71 | + /// </returns> |
| 72 | + /// <exception cref="System.ArgumentNullException"><paramref name="options" /> is <c>null</c>.</exception> |
| 73 | + public static BlobContainerClient CreateBlobContainerClient(this AzureBlobFileSystemOptions options) |
| 74 | + { |
| 75 | + ArgumentNullException.ThrowIfNull(options); |
| 76 | + |
| 77 | + return options.BlobContainerClientFactory(options); |
| 78 | + } |
| 79 | +} |
0 commit comments