Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Storage Read/Write Service
namespace FileUpload.Services
{
public class AzureStorageBlobService : IBlobService
{
private readonly string _connectionString;
public AzureStorageBlobService(string connectionString)
{
_connectionString = connectionString;
}
public async Task<byte[]> GetBlob(string id)
{
var client = new BlobContainerClient(_connectionString, "images");
var blob = client.GetBlobClient(id);
var response = await blob.DownloadAsync();
using (var memStream = new MemoryStream())
{
await response.Value.Content.CopyToAsync(memStream);
return memStream.ToArray();
}
}
public async Task SaveBlob(string id, Stream binaryContents)
{
var client = new BlobContainerClient(_connectionString, "images");
var blob = client.GetBlobClient(id);
await blob.UploadAsync(binaryContents);
}
}
public interface IBlobService
{
Task SaveBlob(string id, Stream binaryContents);
Task<byte[]> GetBlob(string id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment