Skip to content

Commit

Permalink
Added AzureBlobStorage module (simplcommerce#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdavisjr authored and thiennn committed Feb 17, 2018
1 parent 3dff461 commit 0b89300
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
17 changes: 16 additions & 1 deletion SimplCommerce.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2003
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C9BFDDC4-5671-47A3-B57D-197C2A51FA8A}"
EndProject
Expand Down Expand Up @@ -92,6 +92,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.Paymen
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimplCommerce.Module.PaymentStripe", "src\Modules\SimplCommerce.Module.PaymentStripe\SimplCommerce.Module.PaymentStripe.csproj", "{91C58341-5EE8-4FF1-9FAB-BED234C3EBE6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.StorageAzureBlob", "src\Modules\SimplCommerce.Module.StorageAzureBlob\SimplCommerce.Module.StorageAzureBlob.csproj", "{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -546,6 +548,18 @@ Global
{91C58341-5EE8-4FF1-9FAB-BED234C3EBE6}.Release|x64.Build.0 = Release|Any CPU
{91C58341-5EE8-4FF1-9FAB-BED234C3EBE6}.Release|x86.ActiveCfg = Release|Any CPU
{91C58341-5EE8-4FF1-9FAB-BED234C3EBE6}.Release|x86.Build.0 = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|x64.ActiveCfg = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|x64.Build.0 = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|x86.ActiveCfg = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Debug|x86.Build.0 = Debug|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|Any CPU.Build.0 = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|x64.ActiveCfg = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|x64.Build.0 = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|x86.ActiveCfg = Release|Any CPU
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -590,6 +604,7 @@ Global
{A87A5CF6-3FFB-40D0-A82B-0BD29C0F0C65} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
{A43132C2-BD56-4B39-8BDB-280A3230D8C9} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
{91C58341-5EE8-4FF1-9FAB-BED234C3EBE6} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
{21A7421A-DF8B-4E1C-96A8-1FF7FF8EA6F6} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B9D0D8F0-1AB9-44DD-839F-ED8CEE7DDB10}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Extensions.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using SimplCommerce.Module.Core.Services;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimplCommerce.Module.StorageAzureBlob
{
public class AzureBlobStorageService : IStorageService
{
private CloudBlobContainer _blobContainer;

public AzureBlobStorageService(IConfiguration configuration)
{
var storageConnectionString = configuration["Azure:Blob:StorageConnectionString"];
var containerName = configuration["Azure:Blob:ContainerName"];

Contract.Requires(string.IsNullOrWhiteSpace(storageConnectionString));
Contract.Requires(string.IsNullOrWhiteSpace(containerName));

var storageAccount = CloudStorageAccount.Parse(storageConnectionString);

var blobClient = storageAccount.CreateCloudBlobClient();
_blobContainer = blobClient.GetContainerReference(containerName);

}
public async Task DeleteMediaAsync(string fileName)
{
var blockBlob = _blobContainer.GetBlockBlobReference(fileName);
await blockBlob.DeleteIfExistsAsync();
}

public string GetMediaUrl(string fileName)
{
return $"{_blobContainer.Uri.AbsoluteUri}/{fileName}";
}

public async Task SaveMediaAsync(Stream mediaBinaryStream, string fileName, string mimeType = null)
{
await _blobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container });
await _blobContainer.CreateIfNotExistsAsync();

var blockBlob = _blobContainer.GetBlockBlobReference(fileName);

await blockBlob.UploadFromStreamAsync(mediaBinaryStream);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using SimplCommerce.Infrastructure;
using SimplCommerce.Module.Core.Services;

namespace SimplCommerce.Module.StorageAzureBlob
{
public class ModuleInitializer : IModuleInitializer
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{

}

public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<IStorageService, AzureBlobStorageService>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PreserveCompilationContext>false</PreserveCompilationContext>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
<OutputType>Library</OutputType>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="WindowsAzure.Storage" Version="9.0.0" />
<ProjectReference Include="..\..\SimplCommerce.Infrastructure\SimplCommerce.Infrastructure.csproj" />
<ProjectReference Include="..\SimplCommerce.Module.Core\SimplCommerce.Module.Core.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/Modules/SimplCommerce.Module.StorageAzureBlob/module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "storage-azure-blob",
"fullName": "SimplCommerce.Module.StorageAzureBlob",
"version": "1.0.0"
}

0 comments on commit 0b89300

Please sign in to comment.