Skip to content

Commit

Permalink
Added AzureCDN
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Nov 6, 2024
1 parent 83ca43c commit c9d2129
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
7 changes: 2 additions & 5 deletions docs/Media/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The following settings in `appsettings.json` control media upload functionality:
| ConnectionString | string | Azure Storage connection string (only used when AuthenticationMode is `ConnectionString`) |
| ServiceUrl | string | Azure Blob Storage service URL (only used when AuthenticationMode is `Default`) |
| ContainerName | string | Name of the Azure Storage container to store uploaded files. |
| 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. |

## Authentication Methods

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

## Performance Note

Currently, the blog software serves images directly from Azure Blob Storage. For better performance and scalability, consider:

- Using Azure CDN in front of the storage account
- Replacing the blob storage URLs with CDN URLs
- Benefits include HTTP/2 support and improved caching
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.
8 changes: 5 additions & 3 deletions docs/Setup/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ The appsettings.json file has a lot of options to customize the content of the b
"AuthenticationMode": "Default",
"ConnectionString": "",
"ServiceUrl": "",
"ContainerName": ""
},
"ContainerName": "",
"CdnEndpoint": ""
}
}
```

Expand Down Expand Up @@ -106,4 +107,5 @@ The appsettings.json file has a lot of options to customize the content of the b
| AuthenticationMode | string | The authentication mode for the image storage provider. Either `Default` or `ConnectionString` |
| ConnectionString | string | The connection string for the image storage provider. Only used if `AuthenticationMode` is set to `ConnectionString` |
| ServiceUrl | string | The host url of the Azure blob storage. Only used if `AuthenticationMode` is set to `Default` |
| ContainerName | string | The container name for the image storage provider |
| ContainerName | string | The container name for the image storage provider |
| 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. |
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up
}

await blobClient.UploadAsync(fileStream, blobOptions);
return blobClient.Uri.AbsoluteUri;
return GetAssetUrl(blobClient.Uri.ToString(), azureBlobStorageConfiguration.Value);
}

private static BlobServiceClient CreateClient(UploadConfiguration configuration)
Expand All @@ -53,4 +53,18 @@ private static BlobServiceClient CreateClient(UploadConfiguration configuration)

return new BlobServiceClient(new Uri(serviceUrl), new DefaultAzureCredential());
}

private static string GetAssetUrl(string blobUrl, UploadConfiguration config)
{
if (!config.IsCdnEnabled)
{
return blobUrl;
}

var cdnEndpoint = config.CdnEndpoint!.TrimEnd('/');
var blobUri = new Uri(blobUrl);
var path = blobUri.AbsolutePath;

return $"{cdnEndpoint}{path}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class UploadConfiguration
public string? ServiceUrl { get; init; }
public string? ConnectionString { get; init; }
public required string ContainerName { get; init; }
public string? CdnEndpoint { get; init; }
public bool IsCdnEnabled => !string.IsNullOrWhiteSpace(CdnEndpoint);
}
3 changes: 2 additions & 1 deletion src/LinkDotNet.Blog.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"AuthenticationMode": "Default",
"ConnectionString": "",
"ServiceUrl": "",
"ContainerName": ""
"ContainerName": "",
"CdnEndpoint": ""
},
"ShowReadingIndicator": true,
"ShowSimilarPosts": true
Expand Down

0 comments on commit c9d2129

Please sign in to comment.