mirror of https://github.com/abpframework/abp.git
11 changed files with 170 additions and 12 deletions
@ -1,29 +1,72 @@ |
|||
using System.IO; |
|||
using System.Threading.Tasks; |
|||
using Azure.Storage.Blobs; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Azure |
|||
{ |
|||
public class AzureBlobProvider : BlobProviderBase, ITransientDependency |
|||
{ |
|||
public override Task SaveAsync(BlobProviderSaveArgs args) |
|||
public override async Task SaveAsync(BlobProviderSaveArgs args) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), args.BlobName); |
|||
|
|||
if (!args.OverrideExisting && await BlobExistsAsync(blobClient)) |
|||
{ |
|||
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); |
|||
} |
|||
|
|||
public override async Task<bool> DeleteAsync(BlobProviderDeleteArgs args) |
|||
{ |
|||
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), args.BlobName); |
|||
if (await BlobExistsAsync(blobClient)) |
|||
{ |
|||
return (await blobClient.DeleteAsync()).Status == 200; |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args) |
|||
{ |
|||
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), args.BlobName); |
|||
return await BlobExistsAsync(blobClient); |
|||
} |
|||
|
|||
public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args) |
|||
{ |
|||
var blobClient = GetBlobClient(args.Configuration.GetAzureConfiguration(), args.BlobName); |
|||
if (!await BlobExistsAsync(blobClient)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var download = await blobClient.DownloadAsync(); |
|||
|
|||
var memoryStream = new MemoryStream(); |
|||
await download.Value.Content.CopyToAsync(memoryStream); |
|||
return memoryStream; |
|||
} |
|||
|
|||
public override Task<bool> DeleteAsync(BlobProviderDeleteArgs args) |
|||
protected virtual BlobClient GetBlobClient(AzureBlobProviderConfiguration configuration, string blobName) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
var blobContainerClient = GetBlobContainerClient(configuration); |
|||
return blobContainerClient.GetBlobClient(blobName); |
|||
} |
|||
|
|||
public override Task<bool> ExistsAsync(BlobProviderExistsArgs args) |
|||
protected virtual BlobContainerClient GetBlobContainerClient(AzureBlobProviderConfiguration configuration) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
var blobServiceClient = new BlobServiceClient(configuration.ConnectionString); |
|||
return blobServiceClient.GetBlobContainerClient(configuration.ContainerName); |
|||
} |
|||
|
|||
public override Task<Stream> GetOrNullAsync(BlobProviderGetArgs args) |
|||
private static async Task<bool> BlobExistsAsync(BlobClient blobClient) |
|||
{ |
|||
throw new System.NotImplementedException(); |
|||
var response = await blobClient.ExistsAsync(); |
|||
return response.Value; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,8 +1,8 @@ |
|||
namespace Volo.Abp.BlobStoring.Azure |
|||
{ |
|||
public class AzureBlobProviderConfigurationNames |
|||
public static class AzureBlobProviderConfigurationNames |
|||
{ |
|||
|
|||
|
|||
public const string ConnectionString = "Azure.ConnectionString"; |
|||
public const string ContainerName = "Azure.ContainerName"; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,19 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<Import Project="..\..\..\common.test.props" /> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<RootNamespace /> |
|||
<UserSecretsId>9f0d2c00-80c1-435b-bfab-2c39c8249091</UserSecretsId> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.BlobStoring.Azure\Volo.Abp.BlobStoring.Azure.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> |
|||
<ProjectReference Include="..\Volo.Abp.BlobStoring.Tests\Volo.Abp.BlobStoring.Tests.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,12 @@ |
|||
using Volo.Abp.Testing; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Azure |
|||
{ |
|||
public class AbpBlobStoringAzureTestBase : AbpIntegratedTest<AbpBlobStoringAzureTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using Azure.Storage.Blobs; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Volo.Abp.Modularity; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Azure |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpBlobStoringAzureModule), |
|||
typeof(AbpBlobStoringTestModule) |
|||
)] |
|||
public class AbpBlobStoringAzureTestModule : AbpModule |
|||
{ |
|||
private readonly string _randomContainerName = "abp-azure-test-container-" + Guid.NewGuid().ToString("N"); |
|||
|
|||
public override void ConfigureServices(ServiceConfigurationContext context) |
|||
{ |
|||
context.Services.ReplaceConfiguration(ConfigurationHelper.BuildConfiguration(builderAction: builder => |
|||
{ |
|||
builder.AddUserSecrets("9f0d2c00-80c1-435b-bfab-2c39c8249091"); |
|||
})); |
|||
|
|||
var configuration = context.Services.GetConfiguration(); |
|||
|
|||
var blobServiceClient = new BlobServiceClient(configuration["Azure:ConnectionString"]); |
|||
blobServiceClient.CreateBlobContainer(_randomContainerName); |
|||
|
|||
Configure<AbpBlobStoringOptions>(options => |
|||
{ |
|||
options.Containers.ConfigureAll((containerName, containerConfiguration) => |
|||
{ |
|||
containerConfiguration.UseAzure(azure => |
|||
{ |
|||
azure.ConnectionString = configuration["Azure:ConnectionString"]; |
|||
azure.ContainerName = _randomContainerName; |
|||
}); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
public override void OnApplicationShutdown(ApplicationShutdownContext context) |
|||
{ |
|||
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>(); |
|||
var blobServiceClient = new BlobServiceClient(configuration["Azure:ConnectionString"]); |
|||
blobServiceClient.DeleteBlobContainer(_randomContainerName); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Azure |
|||
{ |
|||
public class AzureBlobContainer_Tests : BlobContainer_Tests<AbpBlobStoringAzureTestModule> |
|||
{ |
|||
public AzureBlobContainer_Tests() |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue