Exemple #1
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);
        }
Exemple #2
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
Exemple #3
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);
 }
Exemple #4
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);
        }
Exemple #5
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);
        }