forked from simplcommerce/SimplCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added AzureBlobStorage module (simplcommerce#401)
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Modules/SimplCommerce.Module.StorageAzureBlob/AzureBlobStorageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Modules/SimplCommerce.Module.StorageAzureBlob/ModuleInitializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...odules/SimplCommerce.Module.StorageAzureBlob/SimplCommerce.Module.StorageAzureBlob.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
src/Modules/SimplCommerce.Module.StorageAzureBlob/module.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |