Skip to content

Commit

Permalink
Merge pull request #7 from Luyunmt/master
Browse files Browse the repository at this point in the history
change storage blob track1 to track2
  • Loading branch information
smichelotti committed May 18, 2020
2 parents 12411f7 + 322f1f2 commit 185925c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 57 deletions.
4 changes: 2 additions & 2 deletions IntelligentMission.Web/IntelligentMission.Web.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net462</TargetFramework>
<TargetFramework>net472</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>

Expand All @@ -12,6 +12,7 @@


<ItemGroup>
<PackageReference Include="Azure.Storage.Blobs" Version="12.4.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
Expand All @@ -26,7 +27,6 @@
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
<PackageReference Include="OdeToCode.UseNodeModules" Version="1.0.1" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
<PackageReference Include="WindowsAzure.Storage" Version="8.1.1" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="1.0.0" />
Expand Down
6 changes: 3 additions & 3 deletions IntelligentMission.Web/Services/IStorageClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
using Azure.Storage.Blobs;
using Microsoft.AspNetCore.Http;
using Microsoft.WindowsAzure.Storage.Blob;

namespace IntelligentMission.Web.Services
{
Expand All @@ -12,7 +12,7 @@ public interface IStorageClient
Task DeletePersonFaceBlob(string fullBlobUri);
Task DeleteCatalogFileBlob(string fullBlobUri);
Task DeleteBlobs(string personGroupId, string personId);
CloudBlockBlob GetCatalogFileBlobByUri(string blobUri);
CloudBlockBlob GetAudioEnrollmentBlobByUri(string blobUri);
BlobClient GetCatalogFileBlobByUri(string blobUri);
BlobClient GetAudioEnrollmentBlobByUri(string blobUri);
}
}
19 changes: 7 additions & 12 deletions IntelligentMission.Web/Services/ServiceFactory.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using IntelligentMission.Web.Models;
using Azure.Storage;
using Azure.Storage.Blobs;
using IntelligentMission.Web.Models;
using Microsoft.Azure.Documents.Client;
using Microsoft.ProjectOxford.Face;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -21,15 +20,11 @@ public ServiceFactory(IMConfig config)
this.config = config;
}

public CloudStorageAccount CreateCloudStorageAccount2() =>
new CloudStorageAccount(
new StorageCredentials(
accountName: this.config.StorageConfig.AccountName,
keyValue: this.config.StorageConfig.AccountKey),
endpointSuffix: this.config.StorageConfig.EndpointSuffix,
useHttps: true);
public BlobServiceClient CreateBlobServiceClient2() =>
new BlobServiceClient(new Uri(this.config.StorageConfig.EndpointSuffix),
new StorageSharedKeyCredential( this.config.StorageConfig.AccountName,this.config.StorageConfig.AccountKey));

public CloudBlobClient CreateCloudBlobClient() => this.CreateCloudStorageAccount2().CreateCloudBlobClient();
public BlobServiceClient CreateBlobServiceClient() => this.CreateBlobServiceClient2();

//public FaceServiceClient CreateFaceServiceClient2() => new FaceServiceClient(this.config.Keys.FaceApiKey);
public FaceServiceClient CreateFaceServiceClient2() => new FaceServiceClient(this.config.Keys.FaceApiKey, this.config.CSEndpoints.FaceApi);
Expand Down
19 changes: 11 additions & 8 deletions IntelligentMission.Web/Services/SpeakerIdApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using IntelligentMission.Web.Models;
using Microsoft.WindowsAzure.Storage.Blob;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using IntelligentMission.Web.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
Expand All @@ -16,9 +17,9 @@ namespace IntelligentMission.Web.Services
{
public interface ISpeakerIdApiClient
{
Task<dynamic> Identify(CloudBlockBlob blob);
Task<dynamic> Identify(BlobClient blob);
Task<string> CreateProfile();
Task<dynamic> CreateEnrollment(string identificationProfileId, CloudBlockBlob blob);
Task<dynamic> CreateEnrollment(string identificationProfileId, BlobClient blob);

}
public class SpeakerIdApiClient : ISpeakerIdApiClient
Expand All @@ -43,12 +44,13 @@ public async Task<string> CreateProfile()
}
}

public async Task<dynamic> CreateEnrollment(string identificationProfileId, CloudBlockBlob blob)
public async Task<dynamic> CreateEnrollment(string identificationProfileId, BlobClient blob)
{
using (var httpClient = CreateHttpClient())
using (var stream = new MemoryStream())
{
await blob.DownloadToStreamAsync(stream);
BlobDownloadInfo download = await blob.DownloadAsync();
await download.Content.CopyToAsync(stream);
stream.Position = 0;


Expand All @@ -66,13 +68,14 @@ public async Task<dynamic> CreateEnrollment(string identificationProfileId, Clou
}
}

public async Task<dynamic> Identify(CloudBlockBlob blob)
public async Task<dynamic> Identify(BlobClient blob)
{
// NOTE: API can only support at most 10 speakerIdentificationProfileIds for one identification request so we need to page them
var speakerIdentificationProfileIds = await this.GetValidIdentificationProfileIds();
using (var stream = new MemoryStream())
{
await blob.DownloadToStreamAsync(stream);
BlobDownloadInfo download = await blob.DownloadAsync();
await download.Content.CopyToAsync(stream);
var skipToken = 0;
const int pageSize = 10;
var idSegments = Enumerable.Empty<string>();
Expand Down
56 changes: 29 additions & 27 deletions IntelligentMission.Web/Services/StorageClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using IntelligentMission.Web.Models;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using IntelligentMission.Web.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,12 +12,12 @@ namespace IntelligentMission.Web.Services
{
public class StorageClient : IStorageClient
{
private CloudBlobClient blobClient;
private BlobServiceClient blobServiceClient;
private IMConfig config;

public StorageClient(CloudBlobClient blobClient, IMConfig config)
public StorageClient(BlobServiceClient blobServiceClient, IMConfig config)
{
this.blobClient = blobClient;
this.blobServiceClient = blobServiceClient;
this.config = config;
}

Expand All @@ -36,48 +36,50 @@ public async Task<string> AddNewAudioEnrollmentBlob(IFormFile blob)

private async Task<string> AddBlob(string containerName, string blobName, IFormFile blob)
{
var container = blobClient.GetContainerReference(containerName);
var container = blobServiceClient.GetBlobContainerClient(containerName);
await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
await container.SetAccessPolicyAsync(PublicAccessType.Blob);

var blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.Properties.ContentType = blob.ContentType;
var blockBlob = container.GetBlobClient(blobName);
BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders();
blobHttpHeaders.ContentType = blob.ContentType;

using (var stream = blob.OpenReadStream())
{
await blockBlob.UploadFromStreamAsync(stream);
await blockBlob.UploadAsync(stream, blobHttpHeaders);
}
return blockBlob.Uri.ToString();
}

public async Task<string> AddNewBlob(string personGroupId, string personId, IFormFile blob)
{
var container = blobClient.GetContainerReference(Containers.PersonFaces);
var container = blobServiceClient.GetBlobContainerClient(Containers.PersonFaces);
await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
await container.SetAccessPolicyAsync(PublicAccessType.Blob);

var blobName = GetBlobName(personGroupId, personId, $"{Guid.NewGuid()}-{blob.FileName.Replace(' ', '-')}");
var blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.Properties.ContentType = blob.ContentType;
var blockBlob = container.GetBlobClient(blobName);
BlobHttpHeaders blobHttpHeaders = new BlobHttpHeaders();
blobHttpHeaders.ContentType = blob.ContentType;

using (var stream = blob.OpenReadStream())
{
await blockBlob.UploadFromStreamAsync(stream);
await blockBlob.UploadAsync(stream, blobHttpHeaders);
}
return blockBlob.Uri.ToString();
}

public CloudBlockBlob GetCatalogFileBlobByUri(string blobUri) => GetBlobByUri(blobUri, Containers.CatalogFiles);
public BlobClient GetCatalogFileBlobByUri(string blobUri) => GetBlobByUri(blobUri, Containers.CatalogFiles);


public CloudBlockBlob GetAudioEnrollmentBlobByUri(string blobUri) => GetBlobByUri(blobUri, Containers.AudioEnrollments);
public BlobClient GetAudioEnrollmentBlobByUri(string blobUri) => GetBlobByUri(blobUri, Containers.AudioEnrollments);


private CloudBlockBlob GetBlobByUri(string blobUri, string containerName)
private BlobClient GetBlobByUri(string blobUri, string containerName)
{
var container = blobClient.GetContainerReference(containerName);
var container = blobServiceClient.GetBlobContainerClient(containerName);
var blobName = blobUri.Substring(blobUri.LastIndexOf("/") + 1);
var blob = container.GetBlockBlobReference(blobName);
var blob = container.GetBlobClient(blobName);
return blob;
}

Expand All @@ -93,21 +95,21 @@ public async Task DeleteCatalogFileBlob(string fullBlobUri)

private async Task DeleteBlob(string fullBlobUri, string containerName)
{
var container = blobClient.GetContainerReference(containerName);
var container = blobServiceClient.GetBlobContainerClient(containerName);
var blobName = ExtractBlobNameFromUri(fullBlobUri, containerName);
var blockBlob = container.GetBlockBlobReference(blobName);
var blockBlob = container.GetBlobClient(blobName);
await blockBlob.DeleteAsync();
}

public async Task DeleteBlobs(string personGroupId, string personId)
{
var container = blobClient.GetContainerReference(Containers.PersonFaces);
var container = blobServiceClient.GetBlobContainerClient(Containers.PersonFaces);
var prefix = $"groups/{personGroupId}/persons/{personId}";
var blobs = container.ListBlobs(prefix: prefix, useFlatBlobListing: true);
var blobs = container.GetBlobs(prefix: prefix);

foreach (CloudBlockBlob blobItem in blobs)
foreach (var blobItem in blobs)
{
var blob = container.GetBlockBlobReference(blobItem.Name);
var blob = container.GetBlobClient(blobItem.Name);
await blob.DeleteAsync();
}
}
Expand Down
1 change: 0 additions & 1 deletion IntelligentMission.Web/Services/VisionApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using IntelligentMission.Web.Models;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down
7 changes: 3 additions & 4 deletions IntelligentMission.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
using Microsoft.AspNetCore.Http;
using IntelligentMission.Web.Services;
using IntelligentMission.Web.Models;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using Microsoft.ProjectOxford.Face;
using Microsoft.Azure.Documents.Client;
using Microsoft.AspNetCore.Mvc;
using Azure.Storage.Blobs;

namespace IntelligentMission.Web
{
Expand Down Expand Up @@ -76,8 +75,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<ImageAnalyzer>();
services.AddTransient<PersonManager>();
services.AddTransient<AudioManager>();
services.AddTransient<CloudStorageAccount>(p => p.GetService<ServiceFactory>().CreateCloudStorageAccount2());
services.AddTransient<CloudBlobClient>(p => p.GetService<ServiceFactory>().CreateCloudBlobClient());
services.AddTransient<BlobServiceClient>(p => p.GetService<ServiceFactory>().CreateBlobServiceClient2());
services.AddTransient<BlobServiceClient>(p => p.GetService<ServiceFactory>().CreateBlobServiceClient());
services.AddTransient<FaceServiceClient>(p => p.GetService<ServiceFactory>().CreateFaceServiceClient2());
services.AddTransient<DocumentClient>(p => p.GetService<ServiceFactory>().CreateDocumentClient2());

Expand Down

0 comments on commit 185925c

Please sign in to comment.