using System.IO;
using Microsoft.Samples.ServiceHosting.StorageClient;
namespace newtelligence.DasBlog.Runtime.Azure{
public class StreamDataServiceAzure
{
protected StorageAccountInfo AccountInfo { get; private set; }
public StreamDataServiceAzure()
{
AccountInfo = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
}
public virtual Stream LoadFileStream(string filename, string category)
{
var container = GetContainer(category);
if (!container.DoesBlobExist(filename))
{
return null;
}
Stream stream = new MemoryStream();
BlobContents contents = new BlobContents(stream);
var properties = container.GetBlob(filename, contents, false);
if (properties.ContentLength == 0)
{
return null;
}
return stream;
}
public virtual void SaveFileStream(string filename, string category, Stream file)
{
var container = GetContainer(category);
BlobProperties prop = new BlobProperties(filename);
prop.ContentType = "text/xml; charset=UTF-8";
BlobContents contents = new BlobContents(file);
container.CreateBlob(prop, contents, true);
}
protected BlobContainer GetContainer(string category)
{
BlobStorage store = BlobStorage.Create(AccountInfo);
var container = store.GetBlobContainer(category);
container.CreateContainer();
return container;
}
}
}
|