Exemplo n.º 1
0
        /// <summary>
        /// Returns the implementation of the <see cref="ICloudBlobStorage"/> that corresponds to the Azure storage as defined in the specified location object.
        /// </summary>
        /// <param name="location">The specified location of a blob in the Azure storage infrastructure.</param>
        /// <returns>The object that provides access to the Azure blob storage.</returns>
        public ICloudBlobStorage GetBlobStorage(CloudBlobLocation location)
        {
            Guard.ArgumentNotNull(location, "location");
            Guard.ArgumentNotNull(this.cloudStorageProvider, "cloudStorageProvider");

            return(this.cloudStorageProvider.GetBlobStorage(location.StorageAccount));
        }
Exemplo n.º 2
0
        private XslTransformState PersistInputDocument(Stream data)
        {
            CloudBlobLocation location = this.cloudStorageLoadBalancer.FindBlobLocation();

            if (!CloudBlobLocation.IsUnknown(location))
            {
                using (ICloudBlobStorage blobStorage = this.cloudStorageLoadBalancer.GetBlobStorage(location))
                {
                    string blobName = Guid.NewGuid().ToString("N");

                    if (blobStorage.Put <Stream>(location.ContainerName, blobName, data, true))
                    {
                        return(new XslTransformState(location.StorageAccount, location.ContainerName, blobName));
                    }
                    else
                    {
                        throw new InvalidOperationException(ExceptionMessages.PrepareTransformOperationFailureStreamPersistence);
                    }
                }
            }
            else
            {
                // TODO: Auto-create missing blob container.
                throw new InvalidOperationException(ExceptionMessages.FindBlobLocationFailed);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Suggests the best location for a blob taking into account the load balancing criteria.
        /// </summary>
        /// <returns>The location of a blob in the Azure storage infrastructure.</returns>
        public CloudBlobLocation FindBlobLocation()
        {
            var callToken    = TraceManager.CloudStorageComponent.TraceIn();
            var blobLocation = CloudBlobLocation.Unknown;

            try
            {
                string storageAccountName = null, containerName = null;
                int    minBlobCount = Int32.MaxValue;

                var availableAccounts = from storageAccount in this.storageAccounts.AsParallel <StorageAccountInfo>() select storageAccount;

                // Enumerate through all storage accounts enabled to load balancing.
                foreach (var storageAccount in availableAccounts)
                {
                    // Access the target storage account.
                    using (ICloudBlobStorage blobStorage = this.cloudStorageProvider.GetBlobStorage(storageAccount.AccountName))
                    {
                        // Obtain a list of all containers in the current storage account.
                        var availableContainers = blobStorage.GetContainers(new ContainerRequestOptions()
                        {
                            NamePrefix = ContainerNamePrefix, ListingDetails = ContainerListingDetails.None
                        });

                        // Validate whether or not the current storage account is empty.
                        if (availableContainers != null)
                        {
                            // Enable parallelization when iterating through the collection of container names.
                            var targetContainers = from i in availableContainers.AsParallel <string>() select i;

                            // Enumerate through all containers enabled to load balancing.
                            foreach (var name in targetContainers)
                            {
                                // Obtain the minimum number of blobs in a given container.
                                int newMinValue = Math.Min(minBlobCount, blobStorage.GetCount(name));

                                // When a new minimum is found, capture its value and retain the names of the storage account and container for further reference.
                                if (newMinValue != minBlobCount)
                                {
                                    Interlocked.Exchange <string>(ref storageAccountName, storageAccount.AccountName);
                                    Interlocked.Exchange <string>(ref containerName, name);
                                    Interlocked.Exchange(ref minBlobCount, newMinValue);
                                }
                            }
                        }
                    }
                }

                if (!String.IsNullOrEmpty(storageAccountName) && !String.IsNullOrEmpty(containerName))
                {
                    blobLocation = new CloudBlobLocation(storageAccountName, containerName, null);
                }
            }
            finally
            {
                TraceManager.CloudStorageComponent.TraceOut(callToken, blobLocation.StorageAccount, blobLocation.ContainerName);
            }

            return(blobLocation);
        }