Exemplo n.º 1
0
        private static void UploadText <T>(Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer azureBlobContainer, T export, string blobName)
        {
            var blob = azureBlobContainer.GetBlockBlobReference(blobName);
            var xml  = Serialize <T>(export);

            blob.UploadText(xml);
        }
Exemplo n.º 2
0
        } // SaveBlob

        /// <summary>
        /// Verifies the eTag when saving
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <param name="text"></param>
        /// <param name="eTag"></param>
        public void SaveBlob(string blobContainer, string fileName, string text, string eTag)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            // save
            if (fileName.Contains("."))
            {
                blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf(".")));
            }
            else
            {
                blob.Properties.ContentType = "application/octet-stream";
            }

            Microsoft.WindowsAzure.Storage.AccessCondition accessCondition = new Microsoft.WindowsAzure.Storage.AccessCondition();
            accessCondition.IfMatchETag = eTag;

            PutText(blob, text, accessCondition);
        } // SaveBlob
 static BlobUtility()
 {
     CloudStorage       = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(Startup.AzureStorageConnectionString);
     CloudBlobClient    = CloudStorage.CreateCloudBlobClient();
     CloudBlobContainer = CloudBlobClient.GetContainerReference(Startup.BlobName);
     var res = CloudBlobContainer.CreateIfNotExistsAsync().Result;
 }
Exemplo n.º 4
0
        /// <summary>
        /// Saves to blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <param name="memoryStream"></param>
        public void SaveBlob(string blobContainer, string fileName, MemoryStream memoryStream)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = blobStorageDictionary[containerName];
            blobStorage.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 30, 0);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob      = container.GetBlockBlobReference(fileName);

            // save
            if (fileName.Contains("."))
            {
                blob.Properties.ContentType = GetMimeTypeFromExtension(fileName.Substring(fileName.LastIndexOf(".")));
            }
            else
            {
                blob.Properties.ContentType = "application/octet-stream";
            }

            // save
            memoryStream.Position = 0; // rewind
            blob.UploadFromStream(memoryStream);
        } // SaveBlob
Exemplo n.º 5
0
        /// <summary>
        /// Bild auf Blob-Storage hochladen.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="name"></param>
        /// <param name="container"></param>
        private static void _PutPicture(Stream data, string name, Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container)
        {
            var blob = container.GetBlockBlobReference(name + ".png");

            data.Seek(0, SeekOrigin.Begin);
            blob.UploadFromStream(data);
        }
Exemplo n.º 6
0
        static bool UploadFileWithAzcopyDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName)
        {
            bool bResult = false;

            try
            {
                System.Net.ServicePointManager.Expect100Continue      = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions option = new Microsoft.WindowsAzure.Storage.DataMovement.TransferOptions();

                //option.ParallelOperations = 64;
                //option.MaximumCacheSize = 500000000;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager manager = new Microsoft.WindowsAzure.Storage.DataMovement.TransferManager(option);

                var fileStream = System.IO.File.OpenRead(filesource);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation source      = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(fileStream);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation destination = new Microsoft.WindowsAzure.Storage.DataMovement.TransferLocation(cloudBlob);

                //source.SourceUri = new Uri("file://" + sourceFileName);
                Microsoft.WindowsAzure.Storage.DataMovement.TransferJob job = new Microsoft.WindowsAzure.Storage.DataMovement.TransferJob(source, destination, Microsoft.WindowsAzure.Storage.DataMovement.TransferMethod.SyncCopy);
                System.Threading.CancellationToken token = new System.Threading.CancellationToken();


                //Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UseV1MD5 = false;

                job.ContentType      = contentType;
                job.Starting        += Job_Starting;
                job.ProgressUpdated += Job_ProgressUpdated;
                job.Finished        += Job_Finished;

                Task t = manager.ExecuteJobAsync(job, token);
                t.Wait();
                if (job.IsCompleted == true)
                {
                    bResult = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Exception: " + e.InnerException.Message);
                }
            }
            return(bResult);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets from blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public BlobResultModel GetBlob(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            if (blob.Exists())
            {
                BlobResultModel blobResultModel = new BlobResultModel();

                blobResultModel.Stream = new MemoryStream();
                blob.DownloadToStream(blobResultModel.Stream);
                blobResultModel.eTag = blob.Properties.ETag;

                return(blobResultModel);
            }
            else
            {
                return(null);
            }
        } // GetBlob
Exemplo n.º 8
0
        /// <summary>
        /// Creates the storage and gets a reference (once)
        /// </summary>
        private static void InitializeStorage(string blobContainer)
        {
            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
            {
                return;
            }

            lock (gate)
            {
                if (storageInitializedDictionary.ContainsKey(containerName) && storageInitializedDictionary[containerName] == true)
                {
                    return;
                }

                try
                {
                    Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
                        Sample.Azure.Common.Setting.SettingService.CloudStorageAccountName,
                        Sample.Azure.Common.Setting.SettingService.CloudStorageKey);

                    Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageBlobEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageQueueEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageTableEndPoint),
                                                                                                                                               new Uri(Sample.Azure.Common.Setting.SettingService.CloudStorageFileEndPoint));

                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();

                    int    blobSaveTimeoutInMinutes = DEFAULT_SAVE_AND_READ_BLOB_TIMEOUT_IN_MINUTES;
                    string timeOutOverRide          = Sample.Azure.Common.Setting.SettingService.SaveAndReadBlobTimeoutInMinutes;
                    if (timeOutOverRide != null)
                    {
                        blobSaveTimeoutInMinutes = int.Parse(timeOutOverRide);
                    }
                    blobStorage.DefaultRequestOptions.ServerTimeout = TimeSpan.FromMinutes(blobSaveTimeoutInMinutes);

                    blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
                    Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer cloudBlobContainer = blobStorage.GetContainerReference(containerName);
                    cloudBlobContainer.CreateIfNotExists();

                    Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions permissions = new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions();
                    permissions.PublicAccess = Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Off;
                    cloudBlobContainer.SetPermissions(permissions);

                    blobStorageDictionary.Add(containerName, blobStorage);
                    storageInitializedDictionary.Add(containerName, true);
                }
                catch (Exception ex)
                {
                    throw new Exception("Storage services initialization failure. "
                                        + "Check your storage account configuration settings. If running locally, "
                                        + "ensure that the Development Storage service is running. \n"
                                        + ex.Message);
                }
            } // lock
        }     // InitializeStorage
Exemplo n.º 9
0
        private static async Task DeleteContainer(Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container)
        {
            await container.DeleteIfExistsAsync();

            do
            {
                await Task.Delay(1000);
            } while (await container.ExistsAsync());
        }
Exemplo n.º 10
0
        } // DeleteBlob

        /// <summary>
        /// Gets a list of blobs in the container
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <returns></returns>
        public IEnumerable <Microsoft.WindowsAzure.Storage.Blob.IListBlobItem> ListBlobs(string blobContainer)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            return(container.ListBlobs());
        }
Exemplo n.º 11
0
        /// <summary>
        /// Gets from blob storage
        /// NOTE: If you plan on getting the same blob over and over and quickly saving you will need to throttle and retry
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public BlobResultModel GetBlobAsText(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            return(GetText(blob));
        } // GetBlob
Exemplo n.º 12
0
        } // GetBlob

        /// <summary>
        ///  Saves to blob storage
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        public void DeleteBlob(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            blob.DeleteIfExists();
        } // DeleteBlob
Exemplo n.º 13
0
 public Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer GetBlobContainerObject(string containerName, string accKey)
 {
     Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = Microsoft.WindowsAzure.Storage.CloudStorageAccount.Parse(accKey).CreateCloudBlobClient()
                                                                        .GetContainerReference(containerName.ToLower());
     container.CreateIfNotExists();
     container.SetPermissions(
         new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
     {
         PublicAccess =
             Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Container
     });
     return(container);
 }
        } // Run

        private static string GetSASToken(string containerName, string customerWhitelistIPAddressMinimum, string customerWhitelistIPAddressMaximum,
                                          int tokenExpireTimeInMinutes, string cloudAccountName, string cloudKey)
        {
            Microsoft.WindowsAzure.Storage.Auth.StorageCredentials storageCredentials = new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(cloudAccountName, cloudKey);
            Microsoft.WindowsAzure.Storage.CloudStorageAccount     storageAccount     = null;

            if (cloudAccountName == "devstoreaccount1")
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials,
                                                                                        new Uri("http://127.0.0.1:10000/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10001/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10002/devstoreaccount1"),
                                                                                        new Uri("http://127.0.0.1:10003/devstoreaccount1"));
            }
            else
            {
                storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(storageCredentials, true);
            }

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
            blobStorage.DefaultRequestOptions.RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(1), 10);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
            container.CreateIfNotExistsAsync().Wait();

            Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy policy = new Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPolicy();
            policy.Permissions            = Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.Write | Microsoft.WindowsAzure.Storage.Blob.SharedAccessBlobPermissions.List;
            policy.SharedAccessStartTime  = DateTimeOffset.UtcNow.AddMinutes(-5); // always do in the past to prevent errors
            policy.SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(tokenExpireTimeInMinutes);


            string sasToken = null;

            if (string.IsNullOrWhiteSpace(customerWhitelistIPAddressMinimum) ||
                string.IsNullOrWhiteSpace(customerWhitelistIPAddressMaximum) ||
                cloudAccountName == "devstoreaccount1")
            {
                sasToken = container.GetSharedAccessSignature(policy);
            }
            else
            {
                Microsoft.WindowsAzure.Storage.IPAddressOrRange iPAddressOrRange = new Microsoft.WindowsAzure.Storage.IPAddressOrRange(customerWhitelistIPAddressMinimum, customerWhitelistIPAddressMaximum);
                sasToken = container.GetSharedAccessSignature(policy, null, Microsoft.WindowsAzure.Storage.SharedAccessProtocol.HttpsOnly, iPAddressOrRange);
            }

            //  string url = "https://" + cloudAccountName + ".blob.core.windows.net" + sasToken;

            return(sasToken);
        }
Exemplo n.º 15
0
        static bool UploadFileWithNuggetDLL(string filesource, string contentType, string storageAccountName, string storageAccountKey, string containerName, string blobName)
        {
            bool bResult = false;

            try
            {
                System.Net.ServicePointManager.Expect100Continue      = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = Environment.ProcessorCount * 8;
//                Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.Configurations.ParallelOperations = 64;


                Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(
                    new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(storageAccountName, storageAccountKey), true);

                Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container  = blobClient.GetContainerReference(containerName);

                // Create the container if it doesn't already exist.
                container.CreateIfNotExists();
                Microsoft.WindowsAzure.Storage.Blob.CloudBlob cloudBlob = container.GetBlockBlobReference(blobName);

                Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions options = new Microsoft.WindowsAzure.Storage.DataMovement.UploadOptions();
                options.ContentType = contentType;

                Microsoft.WindowsAzure.Storage.DataMovement.TransferContext context = new Microsoft.WindowsAzure.Storage.DataMovement.TransferContext();
                context.ProgressHandler = new Progress <Microsoft.WindowsAzure.Storage.DataMovement.TransferProgress>((progress) =>
                {
                    Console.WriteLine("Bytes Uploaded: {0}", progress.BytesTransferred);
                });
                StartTime = DateTime.Now;
                Console.WriteLine(string.Format("Starting upload at {0:d/M/yyyy HH:mm:ss.fff}", StartTime));
                // Start the upload
                Task t = Microsoft.WindowsAzure.Storage.DataMovement.TransferManager.UploadAsync(filesource, cloudBlob, options, context);
                t.Wait();
                bResult = true;
                Console.WriteLine(string.Format("Upload finished at {0:d/M/yyyy HH:mm:ss.fff}", DateTime.Now));
                Console.WriteLine(string.Format("Time Elapsed in seconds: " + (DateTime.Now - StartTime).TotalSeconds.ToString()));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Exception: " + e.InnerException.Message);
                }
            }
            return(bResult);
        }
Exemplo n.º 16
0
        } // GetBlob

        /// <summary>
        /// Returns the raw blob handle so you can obtain leases, read data, delete, etc...
        /// </summary>
        /// <param name="blobContainer"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob GetBlobHandle(string blobContainer, string fileName)
        {
            InitializeStorage(blobContainer);

            string containerName = blobContainer.ToString().ToLower(); // must be lower case!

            Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient    blobStorage = blobStorageDictionary[containerName];
            Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container   = blobStorage.GetContainerReference(containerName);
            Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob     blob        = container.GetBlockBlobReference(fileName);

            if (blob.Exists())
            {
                return(blob);
            }
            else
            {
                return(null);
            }
        } // GetBlobHandle
Exemplo n.º 17
0
        public UpdaterFromBlob(string storageConnectionString)
        {
            try
            {
                this._acct = CloudStorageAccount.Parse(storageConnectionString);
            }
            catch (KeyNotFoundException knex)
            {
                // it is development storage
                this._acct = CloudStorageAccount.DevelopmentStorageAccount;
            }

            var blobClnt = this._acct.CreateCloudBlobClient();
            string containerName = CloudConfigurationManager.GetSetting(Constants.SETTING_BLOB_CONTAINER);
            this._container = blobClnt.GetContainerReference(containerName);
            this._container.CreateIfNotExists();
            string leaseContainerName = CloudConfigurationManager.GetSetting(Constants.SETTING_BLOB_CONTAINER_LEASING);
            this._leaseContainer = blobClnt.GetContainerReference(leaseContainerName);
            this._leaseContainer.CreateIfNotExists();

        }
Exemplo n.º 18
0
 public static Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder PersistKeysToAzureBlobStorage(this Microsoft.AspNetCore.DataProtection.IDataProtectionBuilder builder, Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container, string blobName)
 {
     throw null;
 }
Exemplo n.º 19
0
        public string AzureBlopforChunkUpload(System.IO.Stream inputStream, string fileName, string containerName, string accountKey)
        {
            if (fileName != null)
            {
                Repository.AzureBlobUpload objAzureUpload = new Repository.AzureBlobUpload();

                // string accKey = lstBlob[0].Blob_Access_Key;//iConfigPro.GetConfigValue(lstBlob[0].Blob_Access_Key);
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = objAzureUpload.GetBlobContainerObject(containerName, accountKey);
                container.CreateIfNotExists();

                //to replace the file name with new GUID
                //string newfileName = Guid.NewGuid().ToString() + Path.GetExtension(fileName);

                // string newfileName = objBlInfra.GenerateFileName(fileName);

                container.SetPermissions(new Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions {
                    PublicAccess = Microsoft.WindowsAzure.Storage.Blob.BlobContainerPublicAccessType.Blob
                });
                Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

                int blockSize = 1024 * 1024; //256 kb

                using (Stream fileStream = inputStream)
                {
                    long fileSize = fileStream.Length;

                    //block count is the number of blocks + 1 for the last one
                    int blockCount = (int)((float)fileSize / (float)blockSize) + 1;

                    //List of block ids; the blocks will be committed in the order of this list
                    List <string> blockIDs = new List <string>();

                    //starting block number - 1
                    int blockNumber = 0;

                    try
                    {
                        int  bytesRead = 0;        //number of bytes read so far
                        long bytesLeft = fileSize; //number of bytes left to read and upload

                        //do until all of the bytes are uploaded
                        while (bytesLeft > 0)
                        {
                            blockNumber++;
                            int bytesToRead;
                            if (bytesLeft >= blockSize)
                            {
                                //more than one block left, so put up another whole block
                                bytesToRead = blockSize;
                            }
                            else
                            {
                                //less than one block left, read the rest of it
                                bytesToRead = (int)bytesLeft;
                            }

                            //create a blockID from the block number, add it to the block ID list
                            //the block ID is a base64 string
                            string blockId =
                                Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}",
                                                                                                  blockNumber.ToString("0000000"))));
                            blockIDs.Add(blockId);
                            //set up new buffer with the right size, and read that many bytes into it
                            byte[] bytes = new byte[bytesToRead];
                            fileStream.Read(bytes, 0, bytesToRead);

                            //calculate the MD5 hash of the byte array
                            //string blockHash = GetMD5HashFromStream(bytes);

                            var chunkStream = new MemoryStream(bytes);

                            //upload the block, provide the hash so Azure can verify it
                            blob.PutBlock(blockId, chunkStream, null, null,
                                          new Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions()
                            {
                                RetryPolicy = new Microsoft.WindowsAzure.Storage.RetryPolicies.LinearRetry(TimeSpan.FromSeconds(10), 3)
                            },
                                          null);

                            //increment/decrement counters
                            bytesRead += bytesToRead;
                            bytesLeft -= bytesToRead;
                        }

                        //commit the blocks
                        blob.PutBlockList(blockIDs);

                        return(blob.Uri.ToString());
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.Print("Exception thrown = {0}", ex);

                        return(null);
                    }
                }
            }
            return(null);
        }