Skip to content

Instantly share code, notes, and snippets.

@kroymann
Created December 29, 2018 05:24
Show Gist options
  • Select an option

  • Save kroymann/8955c5ebb1d89a04c206f90d92a286c1 to your computer and use it in GitHub Desktop.

Select an option

Save kroymann/8955c5ebb1d89a04c206f90d92a286c1 to your computer and use it in GitHub Desktop.
Sample implementation of the ImageSharp IImageProvider/IImageResolver interfaces that uses an Azure Blob Storage account as the image source
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.WindowsAzure.Storage;
using SixLabors.ImageSharp.Web.Providers;
using SixLabors.ImageSharp.Web.Resolvers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BlobStorageSample
{
public class BlobStorageImageProvider : IImageProvider
{
private readonly BlobStorageImageProviderOptions _options;
private readonly CloudStorageAccount _storageAccount;
public BlobStorageImageProvider(IOptions<BlobStorageImageProviderOptions> options)
{
_options = options.Value;
_storageAccount = CloudStorageAccount.Parse(_options.ConnectionString);
}
public IDictionary<string, string> Settings { get; set; } = new Dictionary<string, string>();
public Func<HttpContext, bool> Match { get; set; } = _ => true;
public bool IsValidRequest(HttpContext context)
{
return !string.IsNullOrEmpty(context.Request.Path.Value);
}
public async Task<IImageResolver> GetAsync(HttpContext context)
{
// Strip the leading slash from the HTTP request path and treat the remaining path string as the blob name.
var blobName = context.Request.Path.Value.TrimStart('\\', '/');
if (string.IsNullOrWhiteSpace(blobName))
{
return null;
}
// Obtain a reference to the desired blob and then make sure it exists
var blobClient = _storageAccount.CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference(_options.ContainerName);
var blob = blobContainer.GetBlockBlobReference(blobName);
if (!await blob.ExistsAsync())
{
return null;
}
// Fetch the metadata so we can access the LastModified time, and then return an IImageResolver that wraps this blob.
await blob.FetchAttributesAsync();
return new BlobStorageImageResolver(blob);
}
}
}
namespace BlobStorageSample
{
public class BlobStorageImageProviderOptions
{
public string ConnectionString { get; set; }
public string ContainerName { get; set; }
}
}
using Microsoft.WindowsAzure.Storage.Blob;
using SixLabors.ImageSharp.Web.Resolvers;
using System;
using System.IO;
using System.Threading.Tasks;
namespace BlobStorageSample
{
public class BlobStorageImageResolver : IImageResolver
{
private readonly CloudBlob _blob;
public BlobStorageImageResolver(CloudBlob blob)
{
_blob = blob;
}
public Task<DateTime> GetLastWriteTimeUtcAsync()
{
return Task.FromResult(_blob.Properties?.LastModified?.DateTime ?? DateTime.UtcNow);
}
public Task<Stream> OpenReadAsync()
{
return _blob.OpenReadAsync();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment