Hi HemaRao,
You can find lots of code snippets and tutorials online for this. Here's one for uploading Blobs:
https://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/
Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
You can use the CloudStorageAccount type and CloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
A CloudBlobClient type allows you to retrieve objects that represent containers and blobs stored within the Blob Storage Service. The following code creates a CloudBlobClient object using the storage account
object we retrieved above:
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
All storage blobs reside in a container. You can use a CloudBlobClient object to get a reference to the container you want to use. You can create the container if it doesn't exist:
// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Create the container if it doesn't already exist
container.CreateIfNotExist();
By default, the new container is private, so you must specify your storage access key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can set the container to be public
using the following code:
container.SetPermissions(
new BlobContainerPermissions { PublicAccess =
BlobContainerPublicAccessType.Blob });
Anyone on the Internet can see blobs in a public container, but you can modify or delete them only if you have the appropriate access key.
To upload a file to a blob, get a container reference and use it to get a blob reference. Once you have a blob reference, you can upload any stream of data to it by calling the UploadFromStream method on the blob reference. This operation
will create the blob if it didn't exist, or overwrite it if it did. The below code sample shows this, and assumes that the container was already created.
// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference("myblob");
// Create or overwrite the "myblob" blob with contents from a local file
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
blob.UploadFromStream(fileStream);
}
To list the blobs in a container, first get a container reference. You can then use the container's ListBlobsmethod to retrieve the blobs within it. The following code demonstrates how to retrieve and output the Uriof
each blob in a container:
// Retrieve storage account from connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
// Loop over blobs within the container and output the URI to each of them
foreach (var blobItem in container.ListBlobs())
{
Console.WriteLine(blobItem.Uri);
}
Hope this helps!
Best Regards,
Carlos Sardo