Created
June 16, 2014 06:07
-
-
Save thecodefish/684f4edb6cbd747aeb85 to your computer and use it in GitHub Desktop.
Azure Blob Storage FileSystemProvider for Umbraco
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
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.IO; | |
using System.Linq; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using Umbraco.Core.IO; | |
namespace MyNamespace | |
{ | |
public class AzureFileSystemProvider : IFileSystem | |
{ | |
private readonly CloudBlobContainer _container; | |
private readonly string _storageUrl; | |
private readonly string _containerName; | |
public AzureFileSystemProvider( | |
string storageUrl, | |
string containerName, | |
string connectionStringName) | |
{ | |
_containerName = containerName; | |
_storageUrl = storageUrl; | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse( | |
ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString); | |
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); | |
_container = blobClient.GetContainerReference(_containerName); | |
if (!_container.Exists()) | |
{ | |
_container.Create(BlobContainerPublicAccessType.Blob); | |
} | |
} | |
public IEnumerable<string> GetDirectories(string path) | |
{ | |
return _container.ListBlobs(path) | |
.Where(b => b is CloudBlobDirectory) | |
.Select(b => b.Uri.ToString()); | |
} | |
public void DeleteDirectory(string path) | |
{ | |
} | |
public void DeleteDirectory(string path, bool recursive) | |
{ | |
} | |
public bool DirectoryExists(string path) | |
{ | |
return _container.GetBlockBlobReference(path).Exists(); | |
} | |
public void AddFile(string path, Stream stream) | |
{ | |
AddFile(path, stream, false); | |
} | |
public void AddFile(string path, Stream stream, bool overrideIfExists) | |
{ | |
CloudBlockBlob blockBlob = _container.GetBlockBlobReference(path); | |
blockBlob.UploadFromStream(stream); | |
} | |
public IEnumerable<string> GetFiles(string path) | |
{ | |
return _container.ListBlobs(path) | |
.Where(b => b is CloudBlockBlob) | |
.Select(b => b.Uri.ToString()); | |
} | |
public IEnumerable<string> GetFiles(string path, string filter) | |
{ | |
return _container.ListBlobs(path) | |
.Where(b => b is CloudBlockBlob) | |
.Select(b => b.Uri.ToString()); | |
} | |
public Stream OpenFile(string path) | |
{ | |
CloudBlockBlob blockBlob = _container.GetBlockBlobReference(path); | |
Stream stream = new MemoryStream(); | |
blockBlob.DownloadToStream(stream); | |
return stream; | |
} | |
public void DeleteFile(string path) | |
{ | |
string azureUrl = path | |
.Replace(Path.DirectorySeparatorChar, '/') | |
.TrimStart('/'); | |
CloudBlockBlob blockBlob = _container.GetBlockBlobReference(azureUrl); | |
blockBlob.Delete(); | |
} | |
public bool FileExists(string path) | |
{ | |
return _container.GetBlockBlobReference(path).Exists(); | |
} | |
public string GetRelativePath(string fullPathOrUrl) | |
{ | |
string path = fullPathOrUrl | |
.Replace( | |
string.Format("{0}/{1}", | |
_storageUrl, | |
_containerName), | |
string.Empty) | |
.Replace('/', Path.DirectorySeparatorChar); | |
return path; | |
} | |
public string GetFullPath(string path) | |
{ | |
string fullPath = string.Format("{0}", | |
path.TrimStart(Path.DirectorySeparatorChar)); | |
return fullPath; | |
} | |
public string GetUrl(string path) | |
{ | |
string url = string.Format("{0}{1}{2}{1}{3}", | |
_storageUrl, | |
Path.DirectorySeparatorChar, | |
_containerName, | |
path | |
) | |
.Replace(Path.DirectorySeparatorChar, '/'); | |
return url; | |
} | |
public DateTimeOffset GetLastModified(string path) | |
{ | |
return DateTimeOffset.UtcNow; | |
} | |
public DateTimeOffset GetCreated(string path) | |
{ | |
return DateTimeOffset.UtcNow; | |
} | |
} | |
} |
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
<?xml version="1.0"?> | |
<FileSystemProviders> | |
<Provider alias="media" type="MyNamespace.AzureFileSystemProvider, MyAssembly"> | |
<Parameters> | |
<add key="storageUrl" value="http://mystorageaccount.blob.core.windows.net" /> | |
<add key="containerName" value="umbraco-media" /> | |
<add key="connectionStringName" value="AzureStorageConnectionString" /> | |
</Parameters> | |
</Provider> | |
</FileSystemProviders> |
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
<connectionStrings> | |
<add name="AzureStorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=mystorageaccount;AccountKey=MyStorageAccountKey" /> | |
</connectionStrings> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment