Ejemplo n.º 1
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
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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
Ejemplo 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
Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
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
Ejemplo n.º 11
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);
        }