private async Task <CloudBlobContainer> CreateCloudBlobContainer( DataProtectionAzureStorageOptions options, CloudStorageAccount cloudStorageAccount, CancellationToken cancellationToken = default) { var sw = Stopwatch.StartNew(); var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); var cloudBlobContainer = cloudBlobClient.GetContainerReference(options.ContainerName); var created = await cloudBlobContainer.CreateIfNotExistsAsync(cancellationToken); if (created) { _logger?.LogInformation(" - No Azure Blob [{containerName}] found - so one was auto created.", options.ContainerName); } else { _logger?.LogInformation(" - Using existing Azure Blob [{containerName}] [{blobName}].", options.ContainerName, options.KeyBlobName); } sw.Stop(); _logger?.LogInformation(" - {nameOf} ran for {seconds} sc", nameof(CreateCloudBlobContainer), sw.Elapsed.TotalSeconds); return(cloudBlobContainer); }
private async Task <CloudBlobContainer> CreateCloudBlobContainer( DataProtectionAzureStorageOptions options, CloudStorageAccount cloudStorageAccount, CancellationToken cancellationToken = default) { var sw = ValueStopwatch.StartNew(); var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); var cloudBlobContainer = cloudBlobClient.GetContainerReference(options.ContainerName); #if NETSTANDARD2_1 var created = await cloudBlobContainer.CreateIfNotExistsAsync(cancellationToken); #elif NETSTANDARD2_0 var created = await cloudBlobContainer.CreateIfNotExistsAsync(); #endif if (created) { _logger.LogInformation("[Azure Blob][DataProtection] No Azure Blob [{blobName}] found - so one was auto created.", options.ContainerName); } else { _logger.LogInformation("[Azure Blob][DataProtection] Using existing Azure Blob:[{blobName}].", options.ContainerName); } _logger.LogInformation("[Azure Blob][DataProtection] Completed: {methodName}; Elapsed: {elapsed}sec", nameof(CreateCloudBlobContainer), sw.GetElapsedTime().TotalSeconds); return(cloudBlobContainer); }
private async Task <CloudStorageAccount> GetStorageAccountAsync( DataProtectionAzureStorageOptions options, CancellationToken cancellationToken = default) { CloudStorageAccount account; if (!string.IsNullOrEmpty(options.ConnectionString) && CloudStorageAccount.TryParse(options.ConnectionString, out var cloudStorageAccount)) { account = cloudStorageAccount; _logger.LogInformation("Azure Storage Authentication with ConnectionString."); } else if (!string.IsNullOrEmpty(options.Name) && string.IsNullOrEmpty(options.Token)) { // Get the initial access token and the interval at which to refresh it. var azureServiceTokenProvider = options.TokenProvider; var tokenAndFrequency = await TokenRenewerAsync( azureServiceTokenProvider, cancellationToken); // Create storage credentials using the initial token, and connect the callback function // to renew the token just before it expires #pragma warning disable CA2000 // Dispose objects before losing scope var tokenCredential = new TokenCredential( tokenAndFrequency.Token, TokenRenewerAsync, azureServiceTokenProvider, #pragma warning disable CS8629 // Nullable value type may be null. tokenAndFrequency.Frequency.Value); #pragma warning restore CS8629 // Nullable value type may be null. #pragma warning restore CA2000 // Dispose objects before losing scope var storageCredentials = new StorageCredentials(tokenCredential); account = new CloudStorageAccount(storageCredentials, options.Name, string.Empty, true); _logger.LogInformation("Azure Storage Authentication with MSI Token."); } else if (!string.IsNullOrEmpty(options.Name) && !string.IsNullOrEmpty(options.Token)) { account = new CloudStorageAccount(new StorageCredentials(options.Token), options.Name, true); _logger.LogInformation("Azure Storage Authentication with SAS Token."); } else { throw new ArgumentException($"One of the following must be set: '{options.ConnectionString}' or '{options.Name}'!"); } return(account); }