using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
namespace GDIShared
{
public class AzureStorageManager
{
private static AzureStorageManager _instance;
private CloudBlobClient _blobClient;
private CloudQueueClient _queueClient;
private AzureStorageManager()
{
}
public static AzureStorageManager Instance
{
get
{
if (_instance == null)
{
_instance = new AzureStorageManager();
_instance.Initialize();
}
return _instance;
}
}
private void Initialize()
{
bool useDevelopmentStorage = true; // modify this to switch between development and cloud storage
if(!useDevelopmentStorage) {
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});
}
CloudStorageAccount storageAccount = useDevelopmentStorage ?
CloudStorageAccount.DevelopmentStorageAccount;
: CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
/*
If you are not using development storage, you'll need to add this setting in ServiceDefinition.csdef
...and this setting in ServiceConfiguration.cscfg:
*/
_blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer publicContainer =
_blobClient.GetContainerReference(Globals.PUBLIC_CONTAINER);
if (publicContainer.CreateIfNotExist())
{
publicContainer.SetPermissions(
new BlobContainerPermissions()
{
PublicAccess = BlobContainerPublicAccessType.Container
});
}
CloudBlobContainer privateContainer =
_blobClient.GetContainerReference(Globals.PRIVATE_CONTAINER);
privateContainer.CreateIfNotExist();
CloudBlobContainer thumbnailContainer =
_blobClient.GetContainerReference(Globals.THUMBNAIL_CONTAINER);
if (thumbnailContainer.CreateIfNotExist())
{
thumbnailContainer.SetPermissions(
new BlobContainerPermissions()
{
PublicAccess = BlobContainerPublicAccessType.Container
});
}
_queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue thumbnailQueue =
_queueClient.GetQueueReference(Globals.THUMBNAIL_QUEUE);
thumbnailQueue.CreateIfNotExist();
}
private void StoreBlob(string blobContainer, string blobName,
string contentType, System.IO.Stream data)
{
try
{
CloudBlob blob =
_blobClient.GetContainerReference(blobContainer)
.GetBlobReference(blobName);
blob.Properties.ContentType = contentType;
blob.UploadFromStream(data);
}
catch (Exception e)
{
string errorMsg = string.Format(
"Error attempting to store blob: {0}", e.Message);
System.Diagnostics.Trace.WriteLine(errorMsg, "Error");
}
}
public void StoreArtwork(string blobContainer, string blobName, string contentType, System.IO.Stream data)
{
StoreBlob(blobContainer, blobName, contentType, data);
QueueThumbnailCreate(blobContainer, blobName);
}
public string GetArtworkContentType(string blobContainer, string blobName)
{
return _blobClient.GetContainerReference(blobContainer)
.GetBlobReference(blobName).Properties.ContentType;
}
public byte[] GetArtworkContent(string blobContainer, string blobName)
{
return _blobClient.GetContainerReference(blobContainer)
.GetBlobReference(blobName).DownloadByteArray();
}
public void Publish(string blobName)
{
CloudBlob src = _blobClient.GetContainerReference(Globals.PRIVATE_CONTAINER)
.GetBlobReference(blobName);
_blobClient.GetContainerReference(Globals.PUBLIC_CONTAINER)
.GetBlobReference(blobName).CopyFromBlob(src);
src.DeleteIfExists();
}
private void QueueThumbnailCreate(string blobContainer,
string blobName)
{
string msg = string.Format("{0}|{1}", blobContainer, blobName);
CloudQueue thumbnailQueue =
_queueClient.GetQueueReference(Globals.THUMBNAIL_QUEUE);
thumbnailQueue.AddMessage(new CloudQueueMessage(msg));
}
public string CheckThumbnailQueue()
{
string thumbnail = null;
CloudQueue thumbnailQueue =
_queueClient.GetQueueReference(Globals.THUMBNAIL_QUEUE);
CloudQueueMessage message = thumbnailQueue.GetMessage();
if (message != null)
{
thumbnail = message.AsString;
thumbnailQueue.DeleteMessage(message);
}
return thumbnail;
}
public string GetThumbnailUrl(string blobName)
{
CloudBlob blob =
_blobClient.GetContainerReference(Globals.THUMBNAIL_CONTAINER)
.GetBlobReference(blobName);
return blob.Uri.ToString();
}
public bool DoesThumbnailExist(string blobName)
{
bool blobExists = false;
CloudBlob blob =
_blobClient.GetContainerReference(Globals.THUMBNAIL_CONTAINER)
.GetBlobReference(blobName);
foreach (var b in
_blobClient.GetContainerReference(Globals.THUMBNAIL_CONTAINER)
.ListBlobs())
{
if (b.Uri.Equals(blob.Uri))
{
blobExists = true;
break;
}
}
return blobExists;
}
public void StoreThumbnail(string blobName, string contentType,
System.IO.Stream data)
{
StoreBlob(Globals.THUMBNAIL_CONTAINER, blobName,
contentType, data);
}
}
}