Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IFileClient
public class AzureBlobFileClient : IFileClient
{
private CloudBlobClient _blobClient;
public AzureBlobFileClient(string connectionString)
{
var account = CloudStorageAccount.Parse(connectionString);
_blobClient = account.CreateCloudBlobClient();
}
public Task DeleteFile(string storeName, string filePath)
{
var container = _blobClient.GetContainerReference(storeName);
var blob = container.GetBlockBlobReference(filePath.ToLower());
return blob.DeleteIfExistsAsync();
}
public Task<bool> FileExists(string storeName, string filePath)
{
var container = _blobClient.GetContainerReference(storeName);
var blob = container.GetBlockBlobReference(filePath.ToLower());
return blob.ExistsAsync();
}
public async Task<Stream> GetFile(string storeName, string filePath)
{
var container = _blobClient.GetContainerReference(storeName);
var blob = container.GetBlockBlobReference(filePath.ToLower());
var mem = new MemoryStream();
await blob.DownloadToStreamAsync(mem).ConfigureAwait(false);
mem.Seek(0, SeekOrigin.Begin);
return mem;
}
public async Task<string> GetFileUrl(string storeName, string filePath)
{
var container = _blobClient.GetContainerReference(storeName);
var blob = container.GetBlockBlobReference(filePath.ToLower());
string url = null;
if(await blob.ExistsAsync().ConfigureAwait(false))
{
url = blob.Uri.AbsoluteUri;
}
return url;
}
public Task SaveFile(string storeName, string filePath, Stream fileStream)
{
var container = _blobClient.GetContainerReference(storeName);
var blob = container.GetBlockBlobReference(filePath.ToLower());
return blob.UploadFromStreamAsync(fileStream);
}
}
public class AzureFileShareClient : IFileClient
{
private CloudFileClient _fileClient;
public AzureFileShareClient(string connectionString)
{
var account = CloudStorageAccount.Parse(connectionString);
_fileClient = account.CreateCloudFileClient();
}
public async Task DeleteFile(string storeName, string filePath)
{
var share = _fileClient.GetShareReference(storeName);
var folder = share.GetRootDirectoryReference();
var pathParts = filePath.Split('/');
var fileName = pathParts[pathParts.Length - 1];
for (var i = 0; i < pathParts.Length - 2; i++)
{
folder = folder.GetDirectoryReference(pathParts[i]);
if(! await folder.ExistsAsync().ConfigureAwait(false))
{
return;
}
}
var fileRef = folder.GetFileReference(fileName);
await fileRef.DeleteIfExistsAsync().ConfigureAwait(false);
}
public async Task<bool> FileExists(string storeName, string filePath)
{
var share = _fileClient.GetShareReference(storeName);
var folder = share.GetRootDirectoryReference();
var pathParts = filePath.Split('/');
var fileName = pathParts[pathParts.Length - 1];
for (var i = 0; i < pathParts.Length - 2; i++)
{
folder = folder.GetDirectoryReference(pathParts[i]);
if (!await folder.ExistsAsync().ConfigureAwait(false))
{
return false;
}
}
var fileRef = folder.GetFileReference(fileName);
return await fileRef.ExistsAsync().ConfigureAwait(false);
}
public async Task<Stream> GetFile(string storeName, string filePath)
{
var share = _fileClient.GetShareReference(storeName);
var folder = share.GetRootDirectoryReference();
var pathParts = filePath.Split('/');
var fileName = pathParts[pathParts.Length - 1];
for (var i = 0; i < pathParts.Length - 2; i++)
{
folder = folder.GetDirectoryReference(pathParts[i]);
if (!await folder.ExistsAsync().ConfigureAwait(false))
{
return null;
}
}
var fileRef = folder.GetFileReference(fileName);
if(!await fileRef.ExistsAsync().ConfigureAwait(false))
{
return null;
}
return await fileRef.OpenReadAsync().ConfigureAwait(false);
}
public Task<string> GetFileUrl(string storeName, string filePath)
{
return Task.FromResult((string)null);
}
public async Task SaveFile(string storeName, string filePath, Stream fileStream)
{
var share = _fileClient.GetShareReference(storeName);
var folder = share.GetRootDirectoryReference();
var pathParts = filePath.Split('/');
var fileName = pathParts[pathParts.Length - 1];
for (var i = 0; i < pathParts.Length - 2; i++)
{
folder = folder.GetDirectoryReference(pathParts[i]);
await folder.CreateIfNotExistsAsync().ConfigureAwait(false);
}
var fileRef = folder.GetFileReference(fileName);
await fileRef.UploadFromStreamAsync(fileStream).ConfigureAwait(false);
}
}
public class LocalFileClient : IFileClient
{
private string _fileRoot;
public LocalFileClient(string fileRoot)
{
_fileRoot = fileRoot;
}
public Task DeleteFile(string storeName, string filePath)
{
var path = Path.Combine(_fileRoot, storeName, filePath);
if(File.Exists(path))
{
File.Delete(path);
}
return Task.CompletedTask;
}
public Task<bool> FileExists(string storeName, string filePath)
{
var path = Path.Combine(_fileRoot, storeName, filePath);
return Task.FromResult(File.Exists(path));
}
public Task<Stream> GetFile(string storeName, string filePath)
{
var path = Path.Combine(_fileRoot, storeName, filePath);
Stream stream = null;
if(File.Exists(path))
{
stream = File.OpenRead(path);
}
return Task.FromResult(stream);
}
public Task<string> GetFileUrl(string storeName, string filePath)
{
return Task.FromResult((string)null);
}
public async Task SaveFile(string storeName, string filePath, Stream fileStream)
{
var path = Path.Combine(_fileRoot, storeName, filePath);
if(File.Exists(path))
{
File.Delete(path);
}
using (var file = new FileStream(path, FileMode.CreateNew))
{
await fileStream.CopyToAsync(file).ConfigureAwait(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.