On the Continuation of the Earlier Article, let us see how to create the Container using C# Programmatically.
1. As usual, let us create a Console application
2. Add the NuGet Packages
a. WindowsAzure.Storage
b. WindowsAzure.ConfigurationManager
3. Make a note of the Access keys.
4. We can use either the key1 or key2. It does not matter.
5. The below code snippet will create the Blob Container.
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.RecordsRepository;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Office365.Console
{
class Program
{
static void Main(string[] args)
{
CreateContainer();
}
public static void CreateContainer()
{
string storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=*************;AccountKey=**********;EndpointSuffix=core.windows.net";
// Parse the connection string and return a reference to the storage account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
//blobClient.
var containers = blobClient.ListContainers();
// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("<<MyContainerName>>");
container.CreateIfNotExists();
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Container});
}
}
}
Happy Coding,
Sathish Nadarajan.
Leave a comment