-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a blob container client factory for customisation (#7849)
* Add a blob container client factory for customisation Allows blob containers to be customised based on grainType and grain id. This is wrapped in an interface to allow injection of dependencies and state to be maintained. * Update src/Azure/Orleans.Persistence.AzureStorage/Providers/Storage/IBlobContainerFactory.cs * Use GrainId since in v4 that's better * Build fixes & minor feedback adjustments * Remove redundant code --------- Co-authored-by: Alex McAuliffe <[email protected]> Co-authored-by: Reuben Bond <[email protected]> Co-authored-by: ReubenBond <[email protected]>
- Loading branch information
1 parent
9e85ac7
commit b24bcc1
Showing
3 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Azure/Orleans.Persistence.AzureStorage/Providers/Storage/IBlobContainerFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Threading.Tasks; | ||
using Azure.Storage.Blobs; | ||
using Orleans.Configuration; | ||
using Orleans.Runtime; | ||
|
||
namespace Orleans.Storage; | ||
|
||
/// <summary> | ||
/// A factory for building container clients for blob storage using grainType and grainId | ||
/// </summary> | ||
public interface IBlobContainerFactory | ||
{ | ||
/// <summary> | ||
/// Gets the container which should be used for the specified grain. | ||
/// </summary> | ||
/// <param name="grainId">The grain id</param> | ||
/// <returns>A configured blob client</returns> | ||
public BlobContainerClient GetBlobContainerClient(GrainId grainId); | ||
|
||
/// <summary> | ||
/// Initialize any required dependencies using the provided client and options. | ||
/// </summary> | ||
/// <param name="client">The connected blob client</param> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
public Task InitializeAsync(BlobServiceClient client); | ||
} | ||
|
||
/// <summary> | ||
/// A default blob container factory that uses the default container name. | ||
/// </summary> | ||
internal class DefaultBlobContainerFactory : IBlobContainerFactory | ||
{ | ||
private readonly AzureBlobStorageOptions _options; | ||
private BlobContainerClient _defaultContainer = null!; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DefaultBlobContainerFactory"/> class. | ||
/// </summary> | ||
/// <param name="options">The blob storage options</param> | ||
public DefaultBlobContainerFactory(AzureBlobStorageOptions options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public BlobContainerClient GetBlobContainerClient(GrainId grainId) | ||
=> _defaultContainer; | ||
|
||
/// <inheritdoc/> | ||
public async Task InitializeAsync(BlobServiceClient client) | ||
{ | ||
_defaultContainer = client.GetBlobContainerClient(_options.ContainerName); | ||
await _defaultContainer.CreateIfNotExistsAsync(); | ||
} | ||
} |