Skip to content
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
80 changes: 49 additions & 31 deletions src/AzureStorageWrapper/AzureStorageWrapper/AzureStorageWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public async Task<BlobReference> UploadBlobAsync(UploadBlob command)

await blobClient.UploadAsync(command.GetContent(), overwrite: true);

command.Metadata ??= new Dictionary<string, string>();
command.Metadata.TryAdd("_timestamp", $"{DateTime.UtcNow}");
if (command.Metadata is null)
{
command.Metadata = new Dictionary<string, string>();
}

command.Metadata["_timestamp"] = $"{DateTime.UtcNow}";

var sanitizedDictionary = SanitizeDictionary(command.Metadata);

await blobClient.SetMetadataAsync(sanitizedDictionary);
Expand Down Expand Up @@ -129,22 +133,27 @@ public async Task<BlobReferenceCollection> EnumerateBlobsAsync(EnumerateBlobs co
var container = GetContainer(command.Container);

var segment = container.GetBlobsAsync().AsPages(command.ContinuationToken, command.Size);

var enumerator = segment.GetAsyncEnumerator();

var references = new List<BlobReference>();

await foreach (var page in segment)
while (await enumerator.MoveNextAsync())
{
var page = enumerator.Current;

foreach (var item in page.Values)
{
var blobReference = await DownloadBlobReferenceAsync(new DownloadBlobReference()
{
Uri = $"{container.Uri}/{item.Name}",
ExpiresIn = _configuration.DefaultSasUriExpiration
});

references.Add(blobReference);
}

await enumerator.DisposeAsync();

return new BlobReferenceCollection()
{
References = references,
Expand All @@ -162,28 +171,35 @@ public async Task<BlobReferenceCollection> EnumerateAllBlobsAsync(EnumerateAllBl
var container = GetContainer(command.Container);

var segment = container.GetBlobsAsync().AsPages(null, 10);

var enumerator = segment.GetAsyncEnumerator();

var references = new List<BlobReference>();

await foreach (var page in segment)
while (await enumerator.MoveNextAsync())
{
var page = enumerator.Current;

foreach (var item in page.Values)
{
var blobReference = await DownloadBlobReferenceAsync(new DownloadBlobReference()
{
Uri = $"{container.Uri}/{item.Name}",
ExpiresIn = _configuration.DefaultSasUriExpiration
});

references.Add(blobReference);
}

await enumerator.DisposeAsync();

return new BlobReferenceCollection()
{
References = references,
ContinuationToken = page.ContinuationToken
};
}

return new BlobReferenceCollection()
{
References = references,
ContinuationToken = null
};
return null;
}

private BlobContainerClient GetContainer(string containerName)
Expand All @@ -207,25 +223,27 @@ private async Task CreateContainerIfNotExists(string containerName)
public async Task<Blob> DownloadBlobAsync(DownloadBlob command)
{
command.Validate();

using var httpClient = new HttpClient();

var response = await httpClient.GetAsync(command.Uri);

if (!response.IsSuccessStatusCode)
using (var httpClient = new HttpClient())
{
var fileName = _uriService.GetFileName(command.Uri);
var fileExtension = _uriService.GetFileExtension(command.Uri);
var response = await httpClient.GetAsync(command.Uri);

if (!response.IsSuccessStatusCode)
{
var fileName = _uriService.GetFileName(command.Uri);
var fileExtension = _uriService.GetFileExtension(command.Uri);

throw new AzureStorageWrapperException(
$"something went wrong when downloading blob {fileName}.{fileExtension}: {response.ReasonPhrase}");
}

var stream = await response.Content.ReadAsStreamAsync();

throw new AzureStorageWrapperException($"something went wrong when downloading blob {fileName}.{fileExtension}: {response.ReasonPhrase}");
return new Blob()
{
Stream = stream
};
}

var stream = await response.Content.ReadAsStreamAsync();

return new Blob()
{
Stream = stream
};
}

private async Task<string> GetSasUriAsync(GetSasUri command)
Expand Down Expand Up @@ -259,7 +277,7 @@ private static Dictionary<string, string> SanitizeDictionary(Dictionary<string,
{
return metadata.ToDictionary(item => SanitizeKey(item.Key), item => SanitizeValue(item.Value));

static string SanitizeKey(string key)
string SanitizeKey(string key)
{
key = RemoveDiacritics(key);

Expand All @@ -268,14 +286,14 @@ static string SanitizeKey(string key)
return key;
}

static string SanitizeValue(string @value)
string SanitizeValue(string @value)
{
value = RemoveDiacritics(value);

return value;
}

static string RemoveDiacritics(string fileName)
string RemoveDiacritics(string fileName)
{
var normalizedString = fileName.Normalize(NormalizationForm.FormD);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<version>2.3.1</version>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<version>2.4.0</version>
<Title>AzureStorageWrapper</Title>
<Authors>Sergio Barriel</Authors>
<Description>Wrapper for Azure Storage, designed to simplify the file upload process and provide links for downloading them. It also supports file deletion and enumeration.</Description>
Expand Down
Loading