+
Skip to content

Init Azure Storage Blob uploader #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/platform/core/mix-heart
1 change: 1 addition & 0 deletions src/platform/mix.constant/Enums/MixStorageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum MixStorageProvider
{
AZURE_STORAGE_BLOB,
CLOUDFLARE,
MIX,
AWS
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mix.library/Services/MixIdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public async Task<JObject> GetOrCreateUserData(MixUser user, CancellationToken c
try
{
var u = await MixDbDataService.GetSingleByParent(MixDatabaseNames.SYSTEM_USER_DATA, MixContentType.User, user.Id, true);
if (u == null)
if (u == null && !GlobalConfigService.Instance.IsInit)
{
u = new JObject()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mix.Storage.Lib.Engines.AzureStorage
{
public sealed class AzureStorageSettings
{
public string StorageAccountName { get; set; }
public string AzureWebJobStorage { get; set; }
public string ContainerName { get; set; }
public string CdnUrl { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Mix.Storage.Lib.Engines.Base;
using Mix.Storage.Lib.Engines.CloudFlare;
using Org.BouncyCastle.Ocsp;

namespace Mix.Storage.Lib.Engines.AzureStorage
{
public class AzureStorageUploader : UploaderBase
{
private readonly BlobContainerClient _blobClient;
private readonly AzureStorageSettings _settings;
public AzureStorageUploader(
IHttpContextAccessor httpContext,
IConfiguration configuration,
UnitOfWorkInfo<MixCmsContext> cmsUow)
: base(httpContext, configuration, cmsUow)
{
_settings = new();
Configuration.Bind("StorageSetting:AzureStorageSetting", _settings);
_blobClient = new BlobContainerClient(_settings.AzureWebJobStorage, _settings.ContainerName);
if (string.IsNullOrEmpty(_settings.CdnUrl))
{
_settings.CdnUrl = $"https://{_settings.StorageAccountName}.blob.core.windows.net";
}

}

public override async Task<string?> Upload(IFormFile file, string? folder, string? createdBy, CancellationToken cancellationToken = default)
{
using (Stream myBlob = file.OpenReadStream())
{
var fileFolder = GetUploadFolder(file.FileName, folder, createdBy);
var fileName = $"{fileFolder}/{file.FileName}";
var blob = _blobClient.GetBlobClient(fileName);
await blob.UploadAsync(myBlob);
return $"{_settings.CdnUrl}/{_settings.ContainerName}/{fileName}";
}
}

public override async Task<string?> UploadStream(FileModel file, string? createdBy, CancellationToken cancellationToken = default)
{
if (file.FileBase64.IsBase64())
{
var fileFolder = GetUploadFolder(file.Extension, file.FolderName, createdBy);
var bytes = Convert.FromBase64String(file.FileBase64.ToBase64Stream());
using (var stream = new MemoryStream(bytes))
{
var fileName = $"{fileFolder}/{file.Filename}{file.Extension}";
var blob = _blobClient.GetBlobClient(fileName);
await _blobClient.UploadBlobAsync(file.Filename, stream, cancellationToken);
return $"{_settings.CdnUrl}/{_settings.ContainerName}/{fileName}";
}
}
throw new MixException("Azure Storage Blob Uploader: Invalid Base64 string");
}

private string GetUploadFolder(string filename, string? fileFolder, string? createdBy)
{
string ext = filename.Split('.')[1].ToLower();
string folder = $"{CurrentTenant.SystemName}/{MixFolders.UploadsFolder}/{ext}";
if (!string.IsNullOrEmpty(fileFolder))
{
folder = $"{folder}/{fileFolder}";
}
if (!string.IsNullOrEmpty(createdBy))
{
folder = $"{folder}/{createdBy}";
}

return $"{folder}/{DateTime.Now.ToString("yyyy-MM")}";
}
}
}
2 changes: 2 additions & 0 deletions src/platform/mix.storage.lib/Services/MixStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Mix.Storage.Lib.Engines.Aws;
using Mix.Storage.Lib.Engines.AzureStorage;
using Mix.Storage.Lib.Engines.Base;
using Mix.Storage.Lib.Engines.CloudFlare;
using Mix.Storage.Lib.Engines.Mix;
Expand Down Expand Up @@ -29,6 +30,7 @@ private IMixUploader CreateUploader(IHttpContextAccessor httpContext, UnitOfWork
MixStorageProvider.CLOUDFLARE => new CloudFlareUploader(httpContext, _configuration, cmsUow,
_httpService),
MixStorageProvider.AWS => new AwsUploader(httpContext, _configuration, cmsUow),
MixStorageProvider.AZURE_STORAGE_BLOB => new AzureStorageUploader(httpContext, _configuration, cmsUow),
MixStorageProvider.MIX => new MixUploader(httpContext, _configuration, cmsUow),
_ => new MixUploader(httpContext, _configuration, cmsUow)
};
Expand Down
4 changes: 1 addition & 3 deletions src/platform/mix.storage.lib/mix.storage.lib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.16.0" />
<PackageReference Include="AWSSDK.S3" Version="3.7.104.15" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mix.library\mix.library.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions src/shared/MixContent/Shared/AppConfigs/storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"ZoneId": "",
"AccountId": ""
},
"AzureStorageSetting": {
"CdnUrl": "",
"StorageAccountName": "",
"AzureWebJobStorage": "",
"ContainerName": ""
},
"MixSetting": {
}
}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载