Last active
August 29, 2015 14:23
-
-
Save refactorsaurusrex/cd942d1ecc76a34d9563 to your computer and use it in GitHub Desktop.
Simple client for uploading a local directory to an Azure blog storage container.
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.IO; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
namespace PublishClickOnceToAzure | |
{ | |
public class AzureBlobClient | |
{ | |
readonly string accessKey; | |
readonly IProgress<string> progress; | |
public AzureBlobClient(string accessKey, IProgress<string> progress) | |
{ | |
this.accessKey = accessKey; | |
this.progress = progress; | |
} | |
public void Push(string containerName, string publishDirectoryPath) | |
{ | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(accessKey); | |
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); | |
CloudBlobContainer container = blobClient.GetContainerReference(containerName); | |
var task = RecreateContainer(container); | |
Task.WaitAll(task); | |
var publishDirectory = new DirectoryInfo(publishDirectoryPath); | |
foreach (FileInfo publishFile in publishDirectory.EnumerateFiles("*", SearchOption.AllDirectories)) | |
{ | |
string blobName = publishFile.FullName.Replace(publishDirectoryPath, ""); | |
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName); | |
using (FileStream fileStream = publishFile.OpenRead()) | |
blockBlob.UploadFromStream(fileStream); | |
} | |
} | |
async Task RecreateContainer(CloudBlobContainer container) | |
{ | |
if (!container.DeleteIfExists()) | |
{ | |
progress.Report("Container successfully created."); | |
container.Create(BlobContainerPublicAccessType.Blob); | |
return; | |
} | |
// Just because the Delete method returned doesn't mean | |
// you can immediately recreate the container. | |
while (true) | |
{ | |
progress.Report("Waiting 10 seconds for container to finish deleting..."); | |
await Task.Delay(10000); | |
try | |
{ | |
// Ensure you do not call Create() with the default | |
// parameters. Doing so will create a private container | |
// which will not be accessible to anyone else. | |
container.Create(BlobContainerPublicAccessType.Blob); | |
progress.Report("Container successfully recreated."); | |
return; | |
} | |
catch (StorageException ex) | |
{ | |
progress.Report(ex.Message); | |
} | |
} | |
} | |
} | |
} |
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.Threading.Tasks; | |
namespace PublishClickOnceToAzure | |
{ | |
class Program | |
{ | |
// Example usage... | |
static void Main(string[] args) | |
{ | |
// container name must be all lower case | |
string containerName = "clickonceapp"; | |
// Make sure to include a trailing slash in the directory path. | |
// Or, make the client code more robust. :-) | |
string publishDirectoryPath = @"C:\Projects\ClickOnceForm\ClickOnceForm\publish\"; | |
// Add your account name and key here. | |
string accessKey = "DefaultEndpointsProtocol=https;AccountName=[youraccountname];AccountKey=[youraccountkey]"; | |
var progress = new Progress<string>(Console.WriteLine); | |
var client = new AzureBlobClient(accessKey, progress); | |
client.Push(containerName, publishDirectoryPath); | |
Console.WriteLine("Done!"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment