public const int DefaultBlockSize           = 4 * 1024 * 1024; // 4MB

        public static Configure AzureDataBus(this Configure config)
        {
            var container = Defaultcontainer;

            CloudBlobClient cloudBlobClient;

            var configSection = Configure.GetConfigSection <AzureDataBusConfig>();

            if (configSection != null)
            {
                cloudBlobClient = CloudStorageAccount.Parse(configSection.ConnectionString).CreateCloudBlobClient();

                container = configSection.Container;
            }
            else
            {
                cloudBlobClient = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudBlobClient();
            }

            var dataBus = new BlobStorageDataBus(cloudBlobClient.GetContainerReference(container));

            if (configSection != null)
            {
                dataBus.BasePath          = configSection.BasePath;
                dataBus.MaxRetries        = configSection.MaxRetries;
                dataBus.NumberOfIOThreads = configSection.NumberOfIOThreads;
                dataBus.BlockSize         = configSection.BlockSize;
            }

            config.Configurer.RegisterSingleton <IDataBus>(dataBus);

            return(config);
        }
        public bool BackUp(string name, string content)
        {
            if (BlobClient == null)
            {
                AzureBlobConfiguration = new AzureBlobConfiguration();
                var connectionString = AzureBlobConfiguration.StorageConnectionStringSettings;
                var storageAccount   = CloudStorageAccount.Parse(connectionString);
                BlobClient = storageAccount.CreateCloudBlobClient();

                BlobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(1), 5);
            }

            var containerReference = AzureBlobConfiguration.StorageContainerReference;
            var container          = BlobClient.GetContainerReference(containerReference);

            if (container == null)
            {
                throw new Exception("Null ContainerReference");
            }

            var blockBlob = container.GetBlockBlobReference(name);

            blockBlob.UploadText(content);
            return(true);
        }
Beispiel #3
0
            public AzureBlobStore(string connectionString)
            {
                var csa        = CloudStorageAccount.Parse(connectionString);
                var blobClient = csa.CreateCloudBlobClient();

                azureGacContainer = blobClient.GetContainerReference(GACContainerName);
                azureGacContainer.CreateIfNotExists();
                Trace.Log.Connected(AppDomain.CurrentDomain.Id, "azure", azureGacContainer.Uri.ToString());
            }
Beispiel #4
0
        private static async Task <CloudBlockBlob> CreateBlobAsync(string blobName)
        {
            var account    = CloudStorageAccount.Parse(_connection);
            var blobClient = account.CreateCloudBlobClient();

            var container = blobClient.GetContainerReference("userdetails");
            await container.CreateIfNotExistsAsync();

            return(container.GetBlockBlobReference(blobName));
        }
Beispiel #5
0
        private static CloudStorageAccount GetStorageAccountFromConfiguration(string name)
        {
            var storageAccountStr = StringHelper.FirstNonEmpty(
                () => CloudConfigurationManager.GetSetting(name),
                () => ConfigurationManager.ConnectionStrings[name].ConnectionString,
                () => ConfigurationManager.AppSettings[name],
                () => { throw new Exception("No storage connection string found."); });

            storageAccountStr = Regex.Replace(storageAccountStr, @"\s+", m => m.Value.Contains("\n") ? "" : m.Value);

            var storageAccount = CloudStorageAccount.Parse(storageAccountStr);

            return(storageAccount);
        }
Beispiel #6
0
 private static void UploadImageToAzure(Item item, AmazonWishList amazonWishList)
 {
     if (amazonWishList.ImageUrl.OriginalString != item.MediumImage.URL)
     {
         using (var webClient = new WebClient())
         {
             var bytes = webClient.DownloadData(item.MediumImage.URL);
             var cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
             var cloudBlobClient     = cloudStorageAccount.CreateCloudBlobClient();
             var cloudBlobContainer  = cloudBlobClient.GetContainerReference(ConfigurationManager.AppSettings["CloudContainerName"]);
             var cloudBlockBlob      = cloudBlobContainer.GetBlockBlobReference(amazonWishList.ImageFileName);
             cloudBlockBlob.UploadFromByteArray(bytes, 0, bytes.Length);
         }
     }
 }
        /// <summary>
        /// Gets a blob reference to work with.
        /// </summary>
        /// <param name="containerName">Name of the container storing the blob.</param>
        /// <param name="blobName">Name of the blob to look for.</param>
        /// <param name="fetchAttributes">Whether to fetch blob attributes or not.</param>
        /// <param name="createContainer">Whether to create the container or not.</param>
        /// <returns>Return the blob object for the given file location.</returns>
        private Blob GetCloudBlockBlob([NotNull] string containerName, [NotNull] string blobName, bool fetchAttributes = false, bool createContainer = false)
        {
            // getting the object representing the blob
            Container container = null;
            Blob      blob;

            if (DebugConfig.IsDebug && !containerName.EndsWith("-debug"))
            {
                containerName += "-debug";
            }

            string fullPath = Path.Combine(containerName, blobName);

            if (!this.blobsMap.TryGetValue(fullPath, out blob))
            {
                // Gets the object representing the container.
                if (!this.containersMap.TryGetValue(containerName, out container))
                {
                    var storageAccountStr = StringHelper.FirstNonEmpty(
                        () => CloudConfigurationManager.GetSetting("StorageConnectionString"),
                        () => ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString,
                        () => ConfigurationManager.AppSettings["StorageConnectionString"],
                        () => { throw new Exception("No storage connection string found."); });

                    storageAccountStr = Regex.Replace(storageAccountStr, @"\s+", m => m.Value.Contains("\n") ? "" : m.Value);

                    var storageAccount = CloudStorageAccount.Parse(storageAccountStr);
                    var blobClient     = storageAccount.CreateCloudBlobClient();
                    var containerRef   = blobClient.GetContainerReference(containerName);
                    this.containersMap[containerName]
                          = container
                          = new Container {
                        CloudBlobContainer = containerRef
                        };
                }

                var blobRef = container.CloudBlobContainer.GetBlockBlobReference(blobName);
                this.blobsMap[fullPath]
                      = blob
                      = new Blob {
                    CloudBlockBlob = blobRef, Container = container,
                    };
            }

            if (container == null)
            {
                container = blob.Container;
            }

            // creating container if needed
            if (createContainer && container.Exists != true)
            {
                if (container.CloudBlobContainer.CreateIfNotExists())
                {
                    container.CloudBlobContainer.SetPermissions(
                        new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Off
                    });
                    blob.Exists = false;
                }

                container.Exists = true;
            }

            // fetching blob attributes
            if (fetchAttributes && blob.Exists != false && blob.AttributesFetched != true)
            {
                bool ok = false;
                try
                {
                    blob.CloudBlockBlob.FetchAttributes();
                    ok = true;
                }
                catch (StorageException e)
                {
                    if (e.ErrorCode != StorageErrorCode.ResourceNotFound)
                    {
                        throw;
                    }
                }
                catch (Microsoft.WindowsAzure.Storage.StorageException e)
                {
                    var webEx = e.InnerException as WebException;
                    if (webEx != null)
                    {
                        var httpWebResponse = webEx.Response as HttpWebResponse;
                        if (httpWebResponse != null && httpWebResponse.StatusCode != HttpStatusCode.NotFound)
                        {
                            throw;
                        }
                    }
                }

                blob.Exists            = ok;
                blob.AttributesFetched = ok;
            }

            return(blob);
        }
Beispiel #8
0
        /// <summary>
        /// Gets a blob reference to work with.
        /// </summary>
        /// <param name="fileLocation">Location of the blob to get a blob object for.</param>
        /// <param name="fetchAttributes">Whether to fetch blob attributes or not.</param>
        /// <param name="createContainer">Whether to create the container or not.</param>
        /// <returns>Return the blob object for the given file location.</returns>
        private Blob GetCloudBlockBlob(string fileLocation, bool fetchAttributes = false, bool createContainer = false)
        {
            // getting the object representing the blob
            Container container = null;
            Blob      blob;

            if (!this.blobsMap.TryGetValue(fileLocation, out blob))
            {
                // Gets the object representing the container.
                var containerName = GetContainerName(fileLocation);

                if (!this.containersMap.TryGetValue(containerName, out container))
                {
                    var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
                    var blobClient     = storageAccount.CreateCloudBlobClient();
                    var containerRef   = blobClient.GetContainerReference(containerName);
                    this.containersMap[containerName]
                          = container
                          = new Container {
                        CloudBlobContainer = containerRef
                        };
                }

                var blobName = GetBlobName(fileLocation);
                var blobRef  = container.CloudBlobContainer.GetBlockBlobReference(blobName);
                this.blobsMap[fileLocation]
                      = blob
                      = new Blob {
                    CloudBlockBlob = blobRef, Container = container,
                    };
            }

            if (container == null)
            {
                container = blob.Container;
            }

            // creating container if needed
            if (createContainer && container.Exists != true)
            {
                if (container.CloudBlobContainer.CreateIfNotExists())
                {
                    container.CloudBlobContainer.SetPermissions(
                        new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Off
                    });
                    blob.Exists = false;
                }

                container.Exists = true;
            }

            // fetching blob attributes
            if (fetchAttributes && blob.Exists != false && blob.AttributesFetched != true)
            {
                bool ok = false;
                try
                {
                    blob.CloudBlockBlob.FetchAttributes();
                    ok = true;
                }
                catch (StorageClientException e)
                {
                    if (e.ErrorCode != StorageErrorCode.ResourceNotFound)
                    {
                        throw;
                    }
                }

                blob.Exists            = ok;
                blob.AttributesFetched = ok;
            }

            return(blob);
        }