Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created June 13, 2021 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/d95a637349d9c5d4bcdc7f6de0ba78bc to your computer and use it in GitHub Desktop.
Save xximjasonxx/d95a637349d9c5d4bcdc7f6de0ba78bc to your computer and use it in GitHub Desktop.
Blob Read/Upload controller
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