Skip to content

Instantly share code, notes, and snippets.

@Tapanila
Created August 18, 2012 16:06
Show Gist options
  • Save Tapanila/3388021 to your computer and use it in GitHub Desktop.
Save Tapanila/3388021 to your computer and use it in GitHub Desktop.
using System.IO;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
namespace WcfServiceExample
{
public class Service1 : IService1
{
string storageConnectionString =
"DefaultEndpointsProtocol=https;AccountName=accountName;" +
"AccountKey=key";
public bool SavePicture(byte[] picture, string pictureName)
{
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container
CloudBlobContainer container =
blobClient.GetContainerReference("pictures");
container.CreateIfNotExist();
// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference(pictureName);
// Create or overwrite the blob with content
var ms = new MemoryStream(picture);
blob.UploadFromStream(ms);
return true;
}
public byte[] RetrievePicture(string pictureName)
{
CloudStorageAccount storageAccount =
CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container
CloudBlobContainer container =
blobClient.GetContainerReference("pictures");
container.CreateIfNotExist();
// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference(pictureName);
// Create or overwrite the blob with content
return ReadFully(blob.OpenRead());
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment