Beispiel #1
0
        public BlobContainer(string name)
        {
            // hämta connectionsträngen från config // RoleEnviroment bestämmer settingvalue runtime
            //var connectionString = RoleEnvironment.GetConfigurationSettingValue("PhotoAppStorage");
            //var connectionString = CloudConfigurationManager.GetSetting("CloudStorageApp");
            // hämtar kontot utfrån connectionsträngens värde
            //var account = CloudStorageAccount.Parse(connectionString);

            //var account = CloudStorageAccount.DevelopmentStorageAccount;

            var cred = new StorageCredentials("jholm",
                "/bVipQ2JxjWwYrZQfHmzhaBx1p1s8BoD/wX6VWOmg4/gpVo/aALrjsDUKqzXsFtc9utepPqe65NposrXt9YsyA==");
            var account = new CloudStorageAccount(cred, true);

            // skapar en blobclient
            _client = account.CreateCloudBlobClient();

            m_BlobContainer = _client.GetContainerReference(name);

            // Om det inte finns någon container med det namnet
            if (!m_BlobContainer.Exists())
            {
                // Skapa containern
                m_BlobContainer.Create();
                var permissions = new BlobContainerPermissions()
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                // Sätter public access till blobs
                m_BlobContainer.SetPermissions(permissions);
            }
        }
        internal void NewContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            Dictionary <string, object> dic = Utility.GenComparisonData(StorageObjectType.Container, NEW_CONTAINER_NAME);
            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            // delete container if it exists
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.DeleteIfExists();

            try
            {
                //--------------New operation--------------
                Test.Assert(agent.NewAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("NewAzureStorageContainer", true));
                // Verification for returned values
                CloudBlobUtil.PackContainerCompareData(container, dic);
                agent.OutputValidation(comp);
                Test.Assert(container.Exists(), "container {0} should exist!", NEW_CONTAINER_NAME);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
        private void InstanciateStorageContainer(string formattedStorageContainerName)
        {
            if ((_blobContainer == null) || (_blobContainer.Name != formattedStorageContainerName))
            {
                var cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString);
                var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

                _blobContainer = cloudBlobClient.GetContainerReference(formattedStorageContainerName);

                if (!_blobContainer.Exists())
                {
                    try
                    {
                        _blobContainer.Create();

                        while (!_blobContainer.Exists())
                            Thread.Sleep(100);
                    }
                    catch (StorageException storageException)
                    {
                        WriteDebugError(String.Format("NLog.AzureStorage - Failed to create Azure Storage Blob Container '{0}' - Storage Exception: {1} {2}", formattedStorageContainerName, storageException.Message, GetStorageExceptionHttpStatusMessage(storageException)));
                        throw;
                    }
                }
            }
        }
Beispiel #4
0
 /// <summary>Constructor establishes the connection</summary>
 /// <param name="containerName">The name of the container inside Azure</param>
 protected BaseStorage(string containerName)
 {
     var cloudStorageAccount = CloudStorageAccount.Parse(
         CloudConfigurationManager.GetSetting(BlobStorageConnectionStringKey));
     var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
     _cloudBlobContainer = cloudBlobClient.GetContainerReference(containerName);
     if (!_cloudBlobContainer.Exists()) _cloudBlobContainer.CreateIfNotExists();
 }
        public BlobTriggerTests()
        {
            _timesProcessed = 0;

            RandomNameResolver nameResolver = new RandomNameResolver();
            _hostConfiguration = new JobHostConfiguration()
            {
                NameResolver = nameResolver,
                TypeLocator = new FakeTypeLocator(typeof(BlobTriggerTests)),
            };

            _storageAccount = CloudStorageAccount.Parse(_hostConfiguration.StorageConnectionString);
            CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();
            _testContainer = blobClient.GetContainerReference(nameResolver.ResolveInString(ContainerName));
            Assert.False(_testContainer.Exists());
            _testContainer.Create();
        }
Beispiel #6
0
        public void CloudBlobClientCreateContainerSharedKeyLite()
        {
            CloudBlobClient blobClient = GenerateCloudBlobClient();

            blobClient.AuthenticationScheme = AuthenticationScheme.SharedKeyLite;

            string             containerName = GetRandomContainerName();
            CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName);

            blobContainer.Create();

            bool exists = blobContainer.Exists();

            Assert.IsTrue(exists);

            blobContainer.Delete();
        }
        internal void RemoveContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

            // create container if it does not exist
            StorageBlob.CloudBlobContainer container = CommonStorageAccount.CreateCloudBlobClient().GetContainerReference(NEW_CONTAINER_NAME);
            container.CreateIfNotExists();

            try
            {
                //--------------Remove operation--------------
                Test.Assert(agent.RemoveAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("RemoveAzureStorageContainer", true));
                Test.Assert(!container.Exists(), "container {0} should not exist!", NEW_CONTAINER_NAME);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
 /// <summary>
 /// Whether the container exists or not
 /// </summary>
 /// <param name="container">A cloudblobcontainer object</param>
 /// <param name="options">Blob request option</param>
 /// <param name="operationContext">Operation context</param>
 /// <returns>True if the specific container exists, otherwise return false</returns>
 public bool DoesContainerExist(CloudBlobContainer container, BlobRequestOptions options, OperationContext operationContext)
 {
     if (null == container)
     {
         return false;
     }
     else
     {
         return container.Exists(options, operationContext);
     }
 }
        private void ResetContainer(CloudBlobContainer container)
        {
            if (!container.Exists()) return;

            Trace.WriteLine($"Ready to parse {container.Name} container");
            Trace.WriteLine("------------------------------------------------");

            var blobs = container.ListBlobs().ToList();

            var total = blobs.Count;
            var counter = 1;

            foreach (var blob in blobs)
            {
                if (blob is CloudBlobDirectory) continue;

                var cloudBlob = (CloudBlob)blob;

                var extension = Path.GetExtension(cloudBlob.Uri.AbsoluteUri);

                string contentType;
                _contentTypes.TryGetValue(extension, out contentType);
                if (string.IsNullOrEmpty(contentType)) continue;

                Trace.Write($"{counter++} of {total} : {cloudBlob.Name}");
                if (cloudBlob.Properties.ContentType == contentType)
                {
                    Trace.WriteLine($" ({cloudBlob.Properties.ContentType}) (skipped)");
                    continue;
                }

                cloudBlob.Properties.ContentType = contentType;
                cloudBlob.SetProperties();
                Trace.WriteLine($" ({cloudBlob.Properties.ContentType}) (reset)");
            }
        }