Blob Read/Upload controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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