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);
        }
示例#2
0
        private CloudBlobContainer EnsurePathIsRelativeAndEnsureContainer(ref string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException();
            }
            path = path.Replace('\\', '/').ToLower().TrimStart('/');
            if (path.StartsWith("http://") || path.StartsWith("https://"))
            {
                throw new ArgumentException("Path must be relative");
            }

            var containerName = path.Split('/').First();
            CloudBlobContainer container;

            lock (_lock)
            {
                container = Containers.SingleOrDefault(c => c.Name == containerName);
                if (container == null)
                {
                    container = BlobClient.GetContainerReference(containerName);
                    if (!container.Exists())
                    {
                        container = ContainerFactory(containerName);
                    }
                    Containers.Add(container);
                }
            }
            path = string.Join("/", path.Split('/').Skip(1).ToArray());
            return(container);
        }
示例#3
0
        private async Task <CloudBlobContainer> GetBlobContainer()
        {
            var container = BlobClient.GetContainerReference(ContainerName);
            await container.CreateIfNotExistsAsync();

            return(container);
        }
示例#4
0
        private async Task Initialzie()
        {
            if (StorageAccount == null)
            {
                // Retrieve storage account from connection string.
                StorageAccount = CloudStorageAccount.Parse(_connectionString);

                // Create the blob client
                BlobClient = StorageAccount.CreateCloudBlobClient();

                // Retrieve reference to a previously created container.
                BlobContainer = BlobClient.GetContainerReference(_originalImageBlobContainerName);

                // Create the container if it doesn't already exist.
                await BlobContainer.CreateIfNotExistsAsync();

                // make access level to blob so urls are public
                var perm = await BlobContainer.GetPermissionsAsync();

                if (perm.PublicAccess != BlobContainerPublicAccessType.Blob)
                {
                    perm.PublicAccess = BlobContainerPublicAccessType.Blob;
                    await BlobContainer.SetPermissionsAsync(perm);
                }
            }
        }
示例#5
0
        private CloudBlobContainer CreateContainer(string name)
        {
            var container = BlobClient.GetContainerReference(name);

            container.CreateIfNotExist();
            return(container);
        }
        public AzureFileSystem(string containerName, string root, bool isPrivate, CloudStorageAccount storageAccount)
        {
            // Setup the connection to custom storage accountm, e.g. Development Storage
            _storageAccount = storageAccount;
            ContainerName   = containerName;
            _root           = String.IsNullOrEmpty(root) ? "": root + "/";
            _absoluteRoot   = Combine(Combine(_storageAccount.BlobEndpoint.AbsoluteUri, containerName), root);

            using (new HttpContextWeaver()) {
                BlobClient = _storageAccount.CreateCloudBlobClient();
                // Get and create the container if it does not exist
                // The container is named with DNS naming restrictions (i.e. all lower case)
                Container = BlobClient.GetContainerReference(ContainerName);

                Container.CreateIfNotExist();

                Container.SetPermissions(isPrivate
                                             ? new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Off
                }
                                             : new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Container
                });
            }
        }
示例#7
0
        private async Task <string> AddImageToContainer(Stream stream, string url, string containerName)
        {
            // reset it to 0
            stream.Seek(0, SeekOrigin.Begin);

            // initialize
            await Initialzie();

            // compose the name
            var blobName = ExtractBlobNameFromUrl(url);

            // get the container
            var container = BlobClient.GetContainerReference(containerName);
            await container.CreateIfNotExistsAsync();

            // make access level to blob so urls are public
            var perm = await container.GetPermissionsAsync();

            if (perm.PublicAccess != BlobContainerPublicAccessType.Blob)
            {
                perm.PublicAccess = BlobContainerPublicAccessType.Blob;
                await container.SetPermissionsAsync(perm);
            }

            // upload the blob
            var blob = container.GetBlockBlobReference(blobName);
            await blob.UploadFromStreamAsync(stream);

            // return the filename
            return($"{container.StorageUri.PrimaryUri}/{blobName}");
        }
示例#8
0
        //download all files
        public void DownlaodAll(string contianerName)
        {
            CloudBlobContainer contianner = BlobClient.GetContainerReference(contianerName);
            string             userName   = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

            string[] arr  = userName.Split('\\');
            string   path = $@"C:\Users\{arr[1]}\Downloads\";
            var      list = contianner.ListBlobs();

            Console.WriteLine(list.Count());
            string[] FilesName = new string[list.Count()];
            int      i         = 0;

            foreach (var blob in list)
            {
                string[] Name = blob.Uri.AbsolutePath.Split('/');
                FilesName[i++] = Name[2];
                Console.WriteLine(Name[2]);
                CloudBlockBlob blockBlob = contianner.GetBlockBlobReference(Name[2]);
                System.IO.Directory.CreateDirectory($@"{path}ImagesPath");
                using (var fileStream = System.IO.File.OpenWrite($@"{path}\ImagesPath\{Name[2]}"))
                {
                    blockBlob.DownloadToStream(fileStream);
                }
            }
        }
示例#9
0
        public BlobStorage(BlobSettings appSettings)
        {
            try
            {
                if (appSettings.ImageSize != null)
                {
                    ResizeLayer = new ResizeLayer(appSettings.ImageSize, ResizeMode.Min);
                }

                UploadThumbnail = appSettings.UploadThumbnail;

                StorageAccountName      = appSettings.StorageAccountName;
                StorageAccountAccessKey = appSettings.StorageAccountAccessKey;

                // Create a blob client and retrieve reference to images container
                BlobClient = StorageAccount.CreateCloudBlobClient();
                Container  = BlobClient.GetContainerReference(appSettings.ContainerName);

                // Create the "images" container if it doesn't already exist.
                if (Container.CreateIfNotExists())
                {
                    // Enable public access on the newly created "images" container
                    Container.SetPermissions(
                        new BlobContainerPermissions
                    {
                        PublicAccess =
                            BlobContainerPublicAccessType.Blob
                    });
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
示例#10
0
        /// <summary>
        /// Downloads the BLOB as byte array.
        /// </summary>
        /// <param name="blobName">Name of the BLOB.</param>
        /// <returns>The array of bytes of the BLOB.</returns>
        public async Task <byte[]> DownloadBlobAsByteArray(string blobName)
        {
            try
            {
                CloudBlobContainer container = BlobClient.GetContainerReference(_containername);
                CloudBlockBlob     blob      = null;
                Uri result;
                if (Uri.TryCreate(blobName, UriKind.Absolute, out result))
                {
                    blob = (CloudBlockBlob)await BlobClient.GetBlobReferenceFromServerAsync(result);
                }
                else
                {
                    blob = container.GetBlockBlobReference(blobName);
                }
                await blob.FetchAttributesAsync();

                byte[] fileByteArray = new byte[blob.Properties.Length];
                await blob.DownloadToByteArrayAsync(fileByteArray, 0);

                return(fileByteArray);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
示例#11
0
        public void BlobDelete(string blobName, string containerName)
        {
            BlobContainer = BlobClient.GetContainerReference(containerName);
            var blockBlob = BlobContainer.GetBlockBlobReference(blobName);

            blockBlob.Delete();
        }
示例#12
0
        public CloudBlobContainer GetContainer(string container)
        {
            var client = BlobClient.GetContainerReference(container);

            client.CreateIfNotExists();
            return(client);
        }
示例#13
0
        public GloopPageData GetPageData(string path)
        {
            if (path[0] == '/')
            {
                path = path.ReplaceFirst("/", "");
            }

            CloudBlobContainer container = BlobClient.GetContainerReference("gloopdata");
            CloudBlobDirectory directory = container.GetDirectoryReference(path);

            var blob = directory.ListBlobs()
                       .OfType <CloudBlob>()
                       .LastOrDefault();

            if (blob == null)
            {
                return(null);
            }

            GloopPageData pageData;

            using (var memoryStream = new MemoryStream())
            {
                blob.DownloadToStream(memoryStream);
                pageData = memoryStream.Deserialize <GloopPageData>();
            }

            return(pageData);
        }
示例#14
0
        private CloudBlobContainer GetContainer(string folder)
        {
            var container = BlobClient.GetContainerReference(folder);

            container.CreateIfNotExistsAsync().Wait();
            return(container);
        }
示例#15
0
        protected virtual async Task CreateTestStorageEntities()
        {
            TestQueue = QueueClient.GetQueueReference(string.Format("test-input-{0}", FixtureId));
            await TestQueue.CreateIfNotExistsAsync();

            await TestQueue.ClearAsync();

            // This queue name should really be suffixed by -fsharp, -csharp, -node etc.
            MobileTablesQueue = QueueClient.GetQueueReference("mobiletables-input");
            await MobileTablesQueue.CreateIfNotExistsAsync(); // do not clear this queue since it is currently shared between fixtures

            TestInputContainer = BlobClient.GetContainerReference(string.Format("test-input-{0}", FixtureId));
            await TestInputContainer.CreateIfNotExistsAsync();

            // Processing a large number of blobs on startup can take a while,
            // so let's start with an empty container.
            await TestHelpers.ClearContainerAsync(TestInputContainer);

            TestOutputContainer = BlobClient.GetContainerReference(string.Format("test-output-{0}", FixtureId));
            await TestOutputContainer.CreateIfNotExistsAsync();

            await TestHelpers.ClearContainerAsync(TestOutputContainer);

            TestTable = TableClient.GetTableReference("test");
            await TestTable.CreateIfNotExistsAsync();

            await DeleteEntities(TestTable, "AAA");
            await DeleteEntities(TestTable, "BBB");

            var batch = new TableBatchOperation();

            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0
            });
            await TestTable.ExecuteBatchAsync(batch);

            batch = new TableBatchOperation();
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0
            });
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0
            });
            await TestTable.ExecuteBatchAsync(batch);
        }
        protected override async Task OnSetAsync(CommandingOrder aggregateRoot)
        {
            var blobContainer = BlobClient.GetContainerReference("orders");
            var blob          = blobContainer.GetBlockBlobReference("order/" + aggregateRoot.Id.ToString());

            var buffer = Serialize(aggregateRoot);
            await blob.UploadFromByteArrayAsync(buffer, 0, buffer.Length);
        }
示例#17
0
        public async Task <CloudBlob> GetorCreateCloudBlobAsync(string containerName, string blobName, BlobType blobType, DateTimeOffset?snapshotTime = null)
        {
            using (Operation azOp = L.Begin("Get Azure Storage blob {0}/{1}", containerName, blobName))
            {
                try
                {
                    GetCloudBlobClient();
                    CloudBlobContainer Container = BlobClient.GetContainerReference(containerName);
                    await Container.CreateIfNotExistsAsync();

                    CloudBlob cloudBlob;
                    switch (blobType)
                    {
                    case BlobType.AppendBlob:
                        cloudBlob = Container.GetAppendBlobReference(blobName, snapshotTime);
                        break;

                    case BlobType.BlockBlob:
                        cloudBlob = Container.GetBlockBlobReference(blobName, snapshotTime);
                        break;

                    case BlobType.PageBlob:
                        cloudBlob = Container.GetPageBlobReference(blobName, snapshotTime);
                        break;

                    case BlobType.Unspecified:
                    default:
                        throw new ArgumentException(string.Format("Invalid blob type {0}", blobType.ToString()), "blobType");
                    }
                    azOp.Complete();
                    return(cloudBlob);
                }
                catch (StorageException se)
                {
                    if (RethrowExceptions)
                    {
                        throw se;
                    }
                    else
                    {
                        L.Error(se, "A storage error occurred getting Azure Storage blob {bn} in container {cn}.", blobName, containerName);
                        return(null);
                    }
                }
                catch (Exception e)
                {
                    if (RethrowExceptions)
                    {
                        throw e;
                    }
                    else
                    {
                        L.Error(e, "An error occurred getting Azure Storage blob {bn} from container {cn}.", blobName, containerName);
                        return(null);
                    }
                }
            }
        }
        protected virtual void CreateTestStorageEntities()
        {
            TestQueue = QueueClient.GetQueueReference(string.Format("test-input-{0}", _testId));
            TestQueue.CreateIfNotExists();
            TestQueue.Clear();

            TestInputContainer = BlobClient.GetContainerReference(string.Format("test-input-{0}", _testId));
            TestInputContainer.CreateIfNotExists();

            TestOutputContainer = BlobClient.GetContainerReference(string.Format("test-output-{0}", _testId));
            TestOutputContainer.CreateIfNotExists();

            TestTable = TableClient.GetTableReference("test");
            TestTable.CreateIfNotExists();

            DeleteEntities(TestTable, "AAA");
            DeleteEntities(TestTable, "BBB");

            var batch = new TableBatchOperation();

            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "001", Region = "West", Name = "Test Entity 1", Status = 0
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "002", Region = "East", Name = "Test Entity 2", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "004", Region = "West", Name = "Test Entity 4", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "AAA", RowKey = "005", Region = "East", Name = "Test Entity 5", Status = 0
            });
            TestTable.ExecuteBatch(batch);

            batch = new TableBatchOperation();
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "001", Region = "South", Name = "Test Entity 1", Status = 0
            });
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "002", Region = "West", Name = "Test Entity 2", Status = 1
            });
            batch.Insert(new TestEntity {
                PartitionKey = "BBB", RowKey = "003", Region = "West", Name = "Test Entity 3", Status = 0
            });
            TestTable.ExecuteBatch(batch);

            string serviceBusQueueName = string.Format("test-input-{0}", _testId);
            string connectionString    = AmbientConnectionStringProvider.Instance.GetConnectionString(ConnectionStringNames.ServiceBus);
            var    namespaceManager    = NamespaceManager.CreateFromConnectionString(connectionString);

            namespaceManager.DeleteQueue(serviceBusQueueName);
            namespaceManager.CreateQueue(serviceBusQueueName);

            ServiceBusQueueClient = Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(connectionString, serviceBusQueueName);
        }
 public AzureFileStorage(IOptions <AzureBlobSettings> fbAuthSettingsAccessor)
 {
     _azureBlobSettings = fbAuthSettingsAccessor.Value;
     StorageAccount     = CloudStorageAccount.Parse(_azureBlobSettings.BlobStorageConnectionString);
     //instantiate the client
     BlobClient = StorageAccount.CreateCloudBlobClient();
     //set the container
     Container = BlobClient.GetContainerReference(_azureBlobSettings.ContainerBlobAzureId);
 }
        protected void SetContainer()
        {
            BlobContainer = BlobClient.GetContainerReference(ContainerName);

            if (CreateIfNotExists)
            {
                BlobContainer.CreateIfNotExistsAsync();
            }
        }
示例#21
0
 public AzureStorage(IOptions <AzureConfig> azureConfig)
 {
     _azureConfig   = azureConfig.Value;
     StorageAccount = CloudStorageAccount.Parse(_azureConfig.StorageConnectionString);
     //instantiate the client
     BlobClient = StorageAccount.CreateCloudBlobClient();
     //set the container
     Container = BlobClient.GetContainerReference(_azureConfig.ImagesContainerName);
 }
 public void Initialize()
 {
     StorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
     BlobClient     = StorageAccount.CreateCloudBlobClient();
     //TODO: Change to appropriate container name
     BlobContainer = BlobClient.GetContainerReference("mycontainer");
     //BlobContainer = BlobClient.GetContainerReference("pdflobs");
     BlobContainer.CreateIfNotExists();
 }
示例#23
0
        protected void SetContainer(string containerName)
        {
            ContainerName = containerName;
            BlobContainer = BlobClient.GetContainerReference(containerName);

            if (CreateIfNotExists)
            {
                BlobContainer.CreateIfNotExists();
            }
        }
示例#24
0
        private AzureStorage()
        {
            StorageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));

            //instantiate the client
            BlobClient = StorageAccount.CreateCloudBlobClient();

            //set the container
            Container = BlobClient.GetContainerReference(ImagecontainerName);
        }
示例#25
0
        /// <summary>
        /// Initialize Windows Azure Storage accounts and CORS settings.
        /// </summary>
        public static void InitializeAccountPropeties()
        {
            BlobClient  = StorageAccount.CreateCloudBlobClient();
            TableClient = StorageAccount.CreateCloudTableClient();

            InitializeCors(BlobClient, TableClient);

            ImagesContainer = BlobClient.GetContainerReference(AzureCommon.ImageContainerName);
            ImagesContainer.CreateIfNotExists(BlobContainerPublicAccessType.Container);
        }
示例#26
0
        private void GetContainer(string ContainerName)
        {
            var storageAccount = CloudStorageAccount.Parse(settings.ConnectionString);

            BlobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            BlobContainer = BlobClient.GetContainerReference(ContainerName);
            // Create the container if it doesn't already exist.
            BlobContainer.CreateIfNotExists();
        }
示例#27
0
        public async Task StoreContent(string path, string contentType, byte[] content)
        {
            path = path.Substring(1, path.Length - 1);
            var container = Environment.GetEnvironmentVariable("Blob.WebContainer");
            CloudBlobContainer cloudBlobContainer = BlobClient.GetContainerReference(container);
            CloudBlockBlob     cloudBlockBlob     = cloudBlobContainer.GetBlockBlobReference(path.ToLowerInvariant());

            cloudBlockBlob.Properties.ContentType = contentType;
            await cloudBlockBlob.UploadFromByteArrayAsync(content, 0, content.Length);
        }
 private CloudBlobContainer GetBlobContainer(string containerName)
 {
     _cloudBlobContainer = BlobClient.GetContainerReference(containerName);
     if (_cloudBlobContainer.CreateIfNotExists())
     {
         BlobContainerPermissions permissions = new BlobContainerPermissions();
         permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
         _cloudBlobContainer.SetPermissions(permissions);
     }
     return(_cloudBlobContainer);
 }
示例#29
0
 public AzureTestContext()
 {
     ContainerName  = Guid.NewGuid().ToString();
     StorageAccount = CloudStorageAccount.Parse(GetConnectionString());
     BlobClient     = StorageAccount.CreateCloudBlobClient();
     Container      = BlobClient.GetContainerReference(ContainerName);
     LocalCache     = new LocalCache();
     LocalSettings  = new LocalSettings();
     FileSystem     = new AzureFileSystem(LocalCache, Uri, StorageAccount, ContainerName);
     Logger         = new TestLogger();
 }
        protected virtual void Initialize(ClientStorageConfig config)
        {
            _config = config;


            BlobClient        = Microsoft.Azure.Storage.CloudStorageAccount.Parse(_config.StorageConnectionString).CreateCloudBlobClient();
            BlobContainerName = config.BlobContainerName;
            if (string.IsNullOrWhiteSpace(BlobContainerName))
            {
                throw new ArgumentException($"BlobContainerName cannot be null or empty, check your configuration.", nameof(config.BlobContainerName));
            }
            ClientBlobContainer = BlobClient.GetContainerReference(BlobContainerName);
        }