Support CreateContainerIfNotExists.

pull/4200/head
maliming 5 years ago
parent 057c4c1a25
commit 3932ea3709

@ -18,39 +18,49 @@ namespace Volo.Abp.BlobStoring.Azure
public override async Task SaveAsync(BlobProviderSaveArgs args)
{
var blobName = AzureBlobNameCalculator.Calculate(args);
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName);
var configuration = args.Configuration.GetAzureConfiguration();
if (!args.OverrideExisting && await BlobExistsAsync(blobClient))
if (!args.OverrideExisting && await BlobExistsAsync(configuration, blobName))
{
throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{args.ContainerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten.");
}
await blobClient.UploadAsync(args.BlobStream, true);
if (configuration.CreateContainerIfNotExists)
{
await CreateContainerIfNotExists(args.Configuration.GetAzureConfiguration());
}
await GetBlobClient(configuration, blobName).UploadAsync(args.BlobStream, true);
}
public override async Task<bool> DeleteAsync(BlobProviderDeleteArgs args)
{
var blobName = AzureBlobNameCalculator.Calculate(args);
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName);
return await blobClient.DeleteIfExistsAsync();
if (await BlobExistsAsync(args.Configuration.GetAzureConfiguration(), blobName))
{
return await GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName).DeleteIfExistsAsync();
}
return false;
}
public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args)
{
var blobName = AzureBlobNameCalculator.Calculate(args);
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName);
return await BlobExistsAsync(blobClient);
return await BlobExistsAsync(args.Configuration.GetAzureConfiguration(), blobName);
}
public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
{
var blobName = AzureBlobNameCalculator.Calculate(args);
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName);
if (!await BlobExistsAsync(blobClient))
if (!await BlobExistsAsync(args.Configuration.GetAzureConfiguration(), blobName))
{
return null;
}
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), blobName);
var download = await blobClient.DownloadAsync();
var memoryStream = new MemoryStream();
await download.Value.Content.CopyToAsync(memoryStream);
@ -69,9 +79,22 @@ namespace Volo.Abp.BlobStoring.Azure
return blobServiceClient.GetBlobContainerClient(configuration.ContainerName);
}
private static async Task<bool> BlobExistsAsync(BlobBaseClient blobClient)
protected virtual async Task CreateContainerIfNotExists(AzureBlobProviderConfiguration configuration)
{
var blobContainerClient = GetBlobContainerClient(configuration);
await blobContainerClient.CreateIfNotExistsAsync();
}
private async Task<bool> BlobExistsAsync(AzureBlobProviderConfiguration configuration, string blobName)
{
// Make sure Blob Container exists.
return await ContainerExistsAsync(GetBlobContainerClient(configuration)) &&
(await GetBlobClient(configuration, blobName).ExistsAsync()).Value;
}
private static async Task<bool> ContainerExistsAsync(BlobContainerClient blobContainerClient)
{
return (await blobClient.ExistsAsync()).Value;
return (await blobContainerClient.ExistsAsync()).Value;
}
}
}

@ -14,6 +14,15 @@
set => _containerConfiguration.SetConfiguration(AzureBlobProviderConfigurationNames.ContainerName, Check.NotNullOrWhiteSpace(value, nameof(value)));
}
/// <summary>
/// Default value: false.
/// </summary>
public bool CreateContainerIfNotExists
{
get => _containerConfiguration.GetConfigurationOrDefault(AzureBlobProviderConfigurationNames.CreateContainerIfNotExists, false);
set => _containerConfiguration.SetConfiguration(AzureBlobProviderConfigurationNames.CreateContainerIfNotExists, value);
}
private readonly BlobContainerConfiguration _containerConfiguration;
public AzureBlobProviderConfiguration(BlobContainerConfiguration containerConfiguration)

@ -4,5 +4,6 @@
{
public const string ConnectionString = "Azure.ConnectionString";
public const string ContainerName = "Azure.ContainerName";
public const string CreateContainerIfNotExists = "Azure.CreateContainerIfNotExists";
}
}

@ -38,12 +38,8 @@ namespace Volo.Abp.BlobStoring.Azure
}));
var configuration = context.Services.GetConfiguration();
_connectionString = configuration["Azure:ConnectionString"];
var blobServiceClient = new BlobServiceClient(_connectionString);
blobServiceClient.CreateBlobContainer(_randomContainerName);
Configure<AbpBlobStoringOptions>(options =>
{
options.Containers.ConfigureAll((containerName, containerConfiguration) =>
@ -52,6 +48,7 @@ namespace Volo.Abp.BlobStoring.Azure
{
azure.ConnectionString = _connectionString;
azure.ContainerName = _randomContainerName;
azure.CreateContainerIfNotExists = true;
});
});
});
@ -60,7 +57,7 @@ namespace Volo.Abp.BlobStoring.Azure
public override void OnApplicationShutdown(ApplicationShutdownContext context)
{
var blobServiceClient = new BlobServiceClient(_connectionString);
blobServiceClient.DeleteBlobContainer(_randomContainerName);
blobServiceClient.GetBlobContainerClient(_randomContainerName).DeleteIfExists();
}
}

Loading…
Cancel
Save