Skip to content

Instantly share code, notes, and snippets.

@NDiiong
Created March 3, 2021 11:57
Show Gist options
  • Save NDiiong/141a2a6c04d5c0cb125bfe108ef932bc to your computer and use it in GitHub Desktop.
Save NDiiong/141a2a6c04d5c0cb125bfe108ef932bc to your computer and use it in GitHub Desktop.
using Azure;
using Azure.Storage.Files.Shares;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AzureFileShare
{
public class AzureFileSharesStorage : IFileStorage
{
private ShareClient _fileShareClient;
public AzureFileSharesStorage(string connectionString, string shareName)
{
_fileShareClient = new ShareClient(connectionString, shareName);
if (!_fileShareClient.Exists())
_fileShareClient.Create();
}
public async Task<bool> CheckFileExistsAsync(string filePath, CancellationToken cancellationToken = default)
{
try
{
var fileClient = GetFileClient(filePath);
return await fileClient.ExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public async Task DeleteFileAsync(string filePath, CancellationToken cancellationToken = default)
{
try
{
var fileClient = GetFileClient(filePath);
_ = await fileClient.DeleteIfExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public async Task<byte[]> GetFileContentAsByteArrayAsync(string filePath, CancellationToken cancellationToken = default)
{
try
{
var fileClient = GetFileClient(filePath);
var download = await fileClient.DownloadAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
using (var stream = new MemoryStream())
{
await download.Value.Content.CopyToAsync(stream).ConfigureAwait(false);
return stream.ToArray();
}
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public async Task<DateTime> GetFileModifiedDateAsync(string filePath, CancellationToken cancellationToken = default)
{
try
{
var fileClient = GetFileClient(filePath);
var fileProperties = await fileClient.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return fileProperties.Value.LastModified.DateTime;
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public async Task<IEnumerable<string>> GetFileNamesAsync(string directory)
{
try
{
var directoryClient = _fileShareClient.GetDirectoryClient(directory);
if (!await directoryClient.ExistsAsync().ConfigureAwait(false))
return Enumerable.Empty<string>();
return directoryClient.GetFilesAndDirectories()
.Where(item => !item.IsDirectory)
.Select(item => item.Name);
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public async Task MoveFileAsync(string sourceFilePath, string destinationFilePath, CancellationToken cancellationToken = default)
{
try
{
var fileClient = GetFileClient(sourceFilePath);
if (await fileClient.ExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
{
using (var stream = await fileClient.OpenReadAsync(cancellationToken: cancellationToken).ConfigureAwait(false))
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
await SaveFileAsync(destinationFilePath, memoryStream.ToArray(), cancellationToken).ConfigureAwait(false);
}
await fileClient.DeleteAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
public async Task SaveFileAsync(string filePath, byte[] fileContent, CancellationToken cancellationToken = default)
{
try
{
var directory = _fileShareClient.GetDirectoryClient(Path.GetDirectoryName(filePath));
if (!await directory.ExistsAsync(cancellationToken).ConfigureAwait(false))
await directory.CreateAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
var file = directory.GetFileClient(Path.GetFileName(filePath));
using (var stream = new MemoryStream(fileContent))
{
await file.CreateAsync(stream.Length, cancellationToken: cancellationToken).ConfigureAwait(false);
await file.UploadRangeAsync(new HttpRange(0, stream.Length), stream, cancellationToken: cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
{
throw new FileStorageException(ex);
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
_fileShareClient = null;
}
private ShareFileClient GetFileClient(string filePath)
{
var dirName = Path.GetDirectoryName(filePath);
var fileName = Path.GetFileName(filePath);
var directory = _fileShareClient.GetDirectoryClient(dirName);
return directory.GetFileClient(fileName);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment