Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created June 13, 2021 00:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xximjasonxx/2664244cf6e87f06ac549812b96ace7d to your computer and use it in GitHub Desktop.
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