Skip to content

Commit

Permalink
- Renamed CreateBlobClient to CreateContainerClient
Browse files Browse the repository at this point in the history
- Added invalidation of client cache if authentication failure
  • Loading branch information
Henning Normann committed Oct 24, 2024
1 parent 32b64e3 commit a22efc4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Storage/Repository/BlobRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public async Task<Stream> ReadBlob(string org, string blobStoragePath, int? stor
_logger.LogWarning("Authentication failed. Invalidating credentials and retrying download operation.");

_memoryCache.Remove(_credsCacheKey);
_memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber));

return await DownloadBlobAsync(org, blobStoragePath, storageContainerNumber);
case "BlobNotFound":
Expand Down Expand Up @@ -92,6 +93,7 @@ public async Task<Stream> ReadBlob(string org, string blobStoragePath, int? stor
_logger.LogWarning("Authentication failed. Invalidating credentials.");

_memoryCache.Remove(_credsCacheKey);
_memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber));

// No use retrying upload as the original stream can't be reset back to start.
throw;
Expand All @@ -116,6 +118,7 @@ public async Task<bool> DeleteBlob(string org, string blobStoragePath, int? stor
_logger.LogWarning("Authentication failed. Invalidating credentials and retrying delete operation.");

_memoryCache.Remove(_credsCacheKey);
_memoryCache.Remove(GetClientCacheKey(org, storageContainerNumber));

return await DeleteIfExistsAsync(org, blobStoragePath, storageContainerNumber);
default:
Expand All @@ -127,7 +130,7 @@ public async Task<bool> DeleteBlob(string org, string blobStoragePath, int? stor
/// <inheritdoc/>
public async Task<bool> DeleteDataBlobs(Instance instance, int? storageContainerNumber)
{
BlobContainerClient container = CreateBlobClient(instance.Org, storageContainerNumber);
BlobContainerClient container = CreateContainerClient(instance.Org, storageContainerNumber);

if (container == null)
{
Expand All @@ -144,7 +147,6 @@ public async Task<bool> DeleteDataBlobs(Instance instance, int? storageContainer
}
catch (Exception e)
{
_memoryCache.Remove(_credsCacheKey);
_logger.LogError(
e,
"BlobService // DeleteDataBlobs // Org: {Instance}",
Expand Down Expand Up @@ -188,14 +190,14 @@ private async Task<bool> DeleteIfExistsAsync(string org, string fileName, int? s

private BlobClient CreateBlobClient(string org, string blobName, int? storageContainerNumber)
{
return CreateBlobClient(org, storageContainerNumber).GetBlobClient(blobName);
return CreateContainerClient(org, storageContainerNumber).GetBlobClient(blobName);
}

private BlobContainerClient CreateBlobClient(string org, int? storageContainerNumber)
private BlobContainerClient CreateContainerClient(string org, int? storageContainerNumber)
{
if (!_storageConfiguration.AccountName.Equals("devstoreaccount1"))
{
string cacheKey = $"blob-{org}-{storageContainerNumber}";
string cacheKey = GetClientCacheKey(org, storageContainerNumber);
if (!_memoryCache.TryGetValue(cacheKey, out BlobContainerClient client))
{
string accountName = string.Format(_storageConfiguration.OrgStorageAccount, org);
Expand Down Expand Up @@ -233,5 +235,10 @@ private TokenCredential GetCachedCredentials()

return creds;
}

private static string GetClientCacheKey(string org, int? storageContainerNumber)
{
return $"blob-{org}-{storageContainerNumber}";
}
}
}

0 comments on commit a22efc4

Please sign in to comment.