Created
April 12, 2016 19:47
-
-
Save jaycdave88/ae8203dde9dff044840672f26fcd3978 to your computer and use it in GitHub Desktop.
Azure code
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 Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using Microsoft.Azure; | |
using System.Collections.Generic; | |
namespace AzurePOC | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Program program = new Program(); | |
ListContainer(null); | |
program.ListBlobs(); | |
} | |
/// <summary> | |
/// Will list the URL of each item within the specific Container | |
/// </summary> | |
public void ListBlobs() | |
{ | |
var containerList = ListContainer(null); | |
foreach (var containerName in containerList) | |
{ | |
var client = GetConfig(); | |
if (client != null) | |
{ | |
var blobClient = GetConfig().BlobClient; | |
var storageAccount = GetConfig().StorageAccount; | |
CloudBlobContainer container = blobClient.GetContainerReference(containerName); | |
foreach (IListBlobItem item in container.ListBlobs(null, false)) | |
{ | |
if (item.GetType() == typeof(CloudBlockBlob)) | |
{ | |
CloudBlockBlob blob = (CloudBlockBlob)item; | |
Console.WriteLine("Block blob length: {0} & URL: {1}", blob.Properties.Length, blob.Uri); | |
ShowSasTokenForContainer(); | |
} | |
else if (item.GetType() == typeof(CloudPageBlob)) | |
{ | |
CloudPageBlob pageBlob = (CloudPageBlob)item; | |
Console.WriteLine("Page blob length: {0} & URL: {1}", pageBlob.Properties.Length, pageBlob.Uri); | |
} | |
else if (item.GetType() == typeof(CloudBlobDirectory)) | |
{ | |
CloudBlobDirectory directory = (CloudBlobDirectory)item; | |
Console.WriteLine("Directory: {0}", directory.Uri); | |
} | |
} | |
} | |
} | |
} | |
/// <summary> | |
/// Start Config will pull the required data and provide it to other methods | |
/// </summary> | |
/// <returns></returns> | |
public static StartConfig GetConfig() | |
{ | |
StartConfig config = new StartConfig(); | |
// Retrieve storage account from connection string. | |
config.StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); | |
// Create the blob object. | |
config.BlobClient = config.StorageAccount.CreateCloudBlobClient(); | |
config.Container = config.BlobClient.GetContainerReference("testingpublicblob"); | |
//Get a reference to a container to use for the sample code, and create it if it does not exist. | |
//Create the container if it does not exisit. | |
config.Container.CreateIfNotExists(); | |
return config; | |
} | |
/// <summary> | |
/// Will return a list of all the containers | |
/// </summary> | |
/// <returns></returns> | |
static public List<string> ListContainer(StartConfig config) | |
{ | |
config = Program.GetConfig(); | |
if(config == null) | |
{ | |
throw new ArgumentNullException("config"); | |
} | |
if(config.BlobClient == null) | |
{ | |
throw new ArgumentException("BlobClient must not be null", "config"); | |
} | |
List<string> container = new List<string>(); | |
//Get the list of the blob from the above container | |
IEnumerable<CloudBlobContainer> containers = config.BlobClient.ListContainers(); | |
foreach (CloudBlobContainer item in containers) | |
{ | |
container.Add(item.Name); | |
config.ContainerNames.Add(String.Join("\n",container)); | |
} | |
//Adding a print statement | |
Console.WriteLine(String.Join("\n", container)); | |
Console.WriteLine("\n"); | |
// Console.WriteLine(startConfig.ContainerNames.ToString()); | |
return container; | |
} | |
// code below this is what is used to add the SAS to existing containers | |
///GET CONTAINER SAS -START | |
/// <summary> | |
/// Shows SAS token for containers | |
/// </summary> | |
static void ShowSasTokenForContainer() | |
{ | |
// Running the method for orgninal config settings. | |
StartConfig config = Program.GetConfig(); | |
// Create a new access policy on the container, which may be optionally used to provide constraints | |
// shared access signatures on the container and the blob. | |
string sharedAccessPolicyName = CloudConfigurationManager.GetSetting("SasPolicyName"); | |
CreateSharedAccessPolicy(config, sharedAccessPolicyName); | |
//generate a SAS token for the container, using a used acces policy to set constraints on the SAS. | |
Console.WriteLine("Container SAS Token using store access policy: {0}\r\n", Program.GetContainersSasTokenWithPolicy(config, sharedAccessPolicyName)); | |
} | |
/// <summary> | |
/// Creates a new Shared Access Policy for the container. | |
/// </summary> | |
/// <param name="blobClient"></param> | |
/// <param name="container"></param> | |
/// <param name="policyName"></param> | |
static void CreateSharedAccessPolicy(StartConfig properties, string policyName) | |
{ | |
//creates a new shared access policy and define its constraints. | |
SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy() | |
{ | |
// policy expiration date | |
SharedAccessExpiryTime = DateTime.UtcNow.AddDays(1), | |
// policy permissions | |
Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List | |
}; | |
//Fetch the container's existing permissions. | |
BlobContainerPermissions permissions = properties.Container.GetPermissions(); | |
//Add new policy to the container's permissions & get container's permissions | |
if (permissions.SharedAccessPolicies.Equals(null)) | |
{ | |
permissions.SharedAccessPolicies.Add(policyName, sharedPolicy); | |
properties.Container.SetPermissions(permissions); | |
} | |
permissions.SharedAccessPolicies.Remove(policyName); | |
} | |
/// <summary> | |
/// Gets the SharedAccessSignature token from the caontiner, using the specifed policy | |
/// </summary> | |
/// <param name="container"></param> | |
/// <param name="policyName"></param> | |
/// <returns></returns> | |
static string GetContainersSasTokenWithPolicy(StartConfig properties, string policyName) | |
{ | |
//generate the sahred access signature on the container. In | |
string sasContainerToken = properties.Container.GetSharedAccessSignature(null, policyName); | |
//return the SAS token' | |
return sasContainerToken; | |
} | |
///GET CONTAINER SAS -END | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment