Skip to content

Commit 5e938a0

Browse files
committed
Added AzureCDN
1 parent 582b7f9 commit 5e938a0

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

docs/Media/Readme.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The following settings in `appsettings.json` control media upload functionality:
2525
| ConnectionString | string | Azure Storage connection string (only used when AuthenticationMode is `ConnectionString`) |
2626
| ServiceUrl | string | Azure Blob Storage service URL (only used when AuthenticationMode is `Default`) |
2727
| ContainerName | string | Name of the Azure Storage container to store uploaded files. |
28+
| CdnEndpoint | string | Optional CDN endpoint to use for uploaded images. If set, the blog will return this URL instead of the storage account URL for uploaded assets. |
2829

2930
## Authentication Methods
3031

@@ -53,8 +54,4 @@ Uses a storage account connection string for authentication:
5354

5455
## Performance Note
5556

56-
Currently, the blog software serves images directly from Azure Blob Storage. For better performance and scalability, consider:
57-
58-
- Using Azure CDN in front of the storage account
59-
- Replacing the blob storage URLs with CDN URLs
60-
- Benefits include HTTP/2 support and improved caching
57+
Use a CDN endpoint for uploaded images to improve performance. This can be set in the `CdnEndpoint` setting. Azure CDN has integrated support for HTTP/2 and bring other performance benefits.

docs/Setup/Configuration.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ The appsettings.json file has a lot of options to customize the content of the b
6363
"AuthenticationMode": "Default",
6464
"ConnectionString": "",
6565
"ServiceUrl": "",
66-
"ContainerName": ""
67-
},
66+
"ContainerName": "",
67+
"CdnEndpoint": ""
68+
}
6869
}
6970
```
7071

@@ -106,4 +107,5 @@ The appsettings.json file has a lot of options to customize the content of the b
106107
| AuthenticationMode | string | The authentication mode for the image storage provider. Either `Default` or `ConnectionString` |
107108
| ConnectionString | string | The connection string for the image storage provider. Only used if `AuthenticationMode` is set to `ConnectionString` |
108109
| ServiceUrl | string | The host url of the Azure blob storage. Only used if `AuthenticationMode` is set to `Default` |
109-
| ContainerName | string | The container name for the image storage provider |
110+
| ContainerName | string | The container name for the image storage provider |
111+
| CdnEndpoint | string | Optional CDN endpoint to use for uploaded images. If set, the blog will return this URL instead of the storage account URL for uploaded assets. |

src/LinkDotNet.Blog.Web/Features/Services/FileUpload/AzureBlobStorageService.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up
3636
}
3737

3838
await blobClient.UploadAsync(fileStream, blobOptions);
39-
return blobClient.Uri.AbsoluteUri;
39+
return GetAssetUrl(blobClient.Uri.ToString(), azureBlobStorageConfiguration.Value);
4040
}
4141

4242
private static BlobServiceClient CreateClient(UploadConfiguration configuration)
@@ -53,4 +53,18 @@ private static BlobServiceClient CreateClient(UploadConfiguration configuration)
5353

5454
return new BlobServiceClient(new Uri(serviceUrl), new DefaultAzureCredential());
5555
}
56+
57+
private static string GetAssetUrl(string blobUrl, UploadConfiguration config)
58+
{
59+
if (!config.IsCdnEnabled)
60+
{
61+
return blobUrl;
62+
}
63+
64+
var cdnEndpoint = config.CdnEndpoint!.TrimEnd('/');
65+
var blobUri = new Uri(blobUrl);
66+
var path = blobUri.AbsolutePath;
67+
68+
return $"{cdnEndpoint}{path}";
69+
}
5670
}

src/LinkDotNet.Blog.Web/Features/Services/FileUpload/UploadConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public class UploadConfiguration
88
public string? ServiceUrl { get; init; }
99
public string? ConnectionString { get; init; }
1010
public required string ContainerName { get; init; }
11+
public string? CdnEndpoint { get; init; }
12+
public bool IsCdnEnabled => !string.IsNullOrWhiteSpace(CdnEndpoint);
1113
}

src/LinkDotNet.Blog.Web/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"AuthenticationMode": "Default",
4242
"ConnectionString": "",
4343
"ServiceUrl": "",
44-
"ContainerName": ""
44+
"ContainerName": "",
45+
"CdnEndpoint": ""
4546
},
4647
"ShowReadingIndicator": true,
4748
"ShowSimilarPosts": true

0 commit comments

Comments
 (0)