Ejemplo n.º 1
0
 public static void PackContainerCompareData(StorageBlob.CloudBlobContainer container, Dictionary <string, object> dic)
 {
     StorageBlob.BlobContainerPermissions permissions = container.GetPermissions();
     dic["PublicAccess"] = permissions.PublicAccess;
     dic["Permission"]   = permissions;
     dic["LastModified"] = container.Properties.LastModified;
 }
Ejemplo n.º 2
0
        public HomeController()
        {
            storageAccount = CloudStorageAccount.Parse(
            ConfigurationManager.AppSettings["StorageConnectionString"]);

            tableClient = storageAccount.CreateCloudTableClient();

            table = tableClient.GetTableReference("fouramigos");

            table.CreateIfNotExists();

            blobClient = storageAccount.CreateCloudBlobClient();

            container = blobClient.GetContainerReference("fouramigos");

            container.CreateIfNotExists();

            BlobContainerPermissions permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            container.SetPermissions(permissions);


            //lägga till nya
            //var tablemodels = new TableModel("Brutus", "Uggla") { Location = "T4", Description="Uggla i träd", Type="Foto" };
            //var tablemodels1 = new TableModel("brutus", "Örn") { Location = "T4", Description="Örn som flyger", Type = "Foto" };

            //var opreation = TableOperation.Insert(tablemodels);
            //var operation2 = TableOperation.Insert(tablemodels1);

            //table.Execute(opreation);
            //table.Execute(operation2);
        }
Ejemplo n.º 3
0
        internal void SetContainerACLTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

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

            comp.Add(dic);

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

            try
            {
                StorageBlob.BlobContainerPublicAccessType[] accessTypes = new StorageBlob.BlobContainerPublicAccessType[] {
                    StorageBlob.BlobContainerPublicAccessType.Blob,
                    StorageBlob.BlobContainerPublicAccessType.Container,
                    StorageBlob.BlobContainerPublicAccessType.Off
                };

                // set PublicAccess as one value respetively
                foreach (var accessType in accessTypes)
                {
                    //--------------Set operation--------------
                    Test.Assert(agent.SetAzureStorageContainerACL(NEW_CONTAINER_NAME, accessType),
                                "SetAzureStorageContainerACL operation should succeed");
                    // Verification for returned values
                    dic["PublicAccess"] = accessType;
                    CloudBlobUtil.PackContainerCompareData(container, dic);
                    agent.OutputValidation(comp);

                    Test.Assert(container.GetPermissions().PublicAccess == accessType,
                                "PublicAccess should be equal: {0} = {1}", container.GetPermissions().PublicAccess, accessType);
                }
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
Ejemplo n.º 4
0
		/// <summary>
		/// Creates a new BlobStorage object
		/// </summary>
		/// <param name="blobContainerName">The name of the blob to be managed</param>
		/// <param name="storageConnectionString">The connection string pointing to the storage account (this can be local or hosted in Windows Azure</param>
		public BlobStorageAsync(string blobContainerName, string storageConnectionString)
		{
			Validate.BlobContainerName(blobContainerName, "blobContainerName");
			Validate.String(storageConnectionString, "storageConnectionString");

			var cloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString);
			var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

			cloudBlobContainer = cloudBlobClient.GetContainerReference(blobContainerName);
			cloudBlobContainer.CreateIfNotExists();

			var permissions = cloudBlobContainer.GetPermissions();
			permissions.PublicAccess = BlobContainerPublicAccessType.Container;
			cloudBlobContainer.SetPermissions(permissions);
		}
Ejemplo n.º 5
0
        public void CloudBlobContainerSetPermissions()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                BlobContainerPermissions permissions = container.GetPermissions();
                Assert.AreEqual(BlobContainerPublicAccessType.Off, permissions.PublicAccess);
                Assert.AreEqual(0, permissions.SharedAccessPolicies.Count);

                // We do not have precision at milliseconds level. Hence, we need
                // to recreate the start DateTime to be able to compare it later.
                DateTime start = DateTime.UtcNow;
                start = new DateTime(start.Year, start.Month, start.Day, start.Hour, start.Minute, start.Second, DateTimeKind.Utc);
                DateTime expiry = start.AddMinutes(30);

                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                permissions.SharedAccessPolicies.Add("key1", new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = SharedAccessBlobPermissions.List,
                });
                container.SetPermissions(permissions);
                Thread.Sleep(30 * 1000);

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                permissions = container2.GetPermissions();
                Assert.AreEqual(BlobContainerPublicAccessType.Container, permissions.PublicAccess);
                Assert.AreEqual(1, permissions.SharedAccessPolicies.Count);
                Assert.IsTrue(permissions.SharedAccessPolicies["key1"].SharedAccessStartTime.HasValue);
                Assert.AreEqual(start, permissions.SharedAccessPolicies["key1"].SharedAccessStartTime.Value.UtcDateTime);
                Assert.IsTrue(permissions.SharedAccessPolicies["key1"].SharedAccessExpiryTime.HasValue);
                Assert.AreEqual(expiry, permissions.SharedAccessPolicies["key1"].SharedAccessExpiryTime.Value.UtcDateTime);
                Assert.AreEqual(SharedAccessBlobPermissions.List, permissions.SharedAccessPolicies["key1"].Permissions);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        /// <summary>
        /// Creates a new Shared Access Policy for the container
        /// </summary>
        /// <param name="blobClient">The blob client to use for this operation</param>
        /// <param name="container">The container to create the policy for</param>
        /// <param name="policyName">The name of the policy</param>
        static void CreateSharedAccessPolicy(CloudBlobClient blobClient, CloudBlobContainer container,
    string policyName)
        {
            //Create a new shared access policy and define its constraints.
            SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy()
            {
                // What is the expiration date of this policy?
                SharedAccessExpiryTime = DateTime.UtcNow.AddYears(1),
                // What permissions does this policy grant?
                Permissions = SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.List
            };

            //Get the container's existing permissions.
            BlobContainerPermissions permissions = container.GetPermissions();

            //Add the new policy to the container's permissions, and set the container's permissions.
            permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
            container.SetPermissions(permissions);
        }
Ejemplo n.º 7
0
        //Initializing constructor...
        public AzureContext(string appSettingsKey) 
        {
            if ( null == _cloudStorageAccount ) //ASSUMPTION: This member is ALWAYS allocated first...
            {
                lock (lockObject)
                {
                    if ( null == _cloudStorageAccount )
                    {
                        string connectionString = ConfigurationManager.AppSettings[appSettingsKey];
                        _cloudStorageAccount = CloudStorageAccount.Parse(connectionString);

                        _cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient();

                        _cloudBlobContainer = _cloudBlobClient.GetContainerReference(ConfigurationManager.AppSettings["blobContainer"]);

                        //ASSUMPTION: container access settings: public read access blobs only...
                        BlobContainerPermissions bcp = _cloudBlobContainer.GetPermissions();

                        Debug.Assert(BlobContainerPublicAccessType.Blob == bcp.PublicAccess);
                    }
                }
            }
        }
        public void ProcessMedia()
        {
            // Run the thumbnail job.
            var asset = RunVideoThumbnailJob((string)_mediaObject["mediaName"], "config.json");

            if (asset != null)
            {
                Uri blobUri = new Uri(asset.Uri.ToString());
                Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container = new Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer(blobUri, _blobCredentials);

                BlobContainerPermissions permissions = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);
                var summaryVideoUrl = container.Uri.OriginalString + "/" + asset.AssetFiles.FirstOrDefault().Name;
                _log.Info($"Granted public access to {summaryVideoUrl}");

                _dbContext.UpdateSummaryUrl((int)_mediaObject["mediaId"], summaryVideoUrl);
            }
            else
            {
                _log.Info($"No assets found for media name: {_mediaObject["mediaName"]}");
            }
        }
 /// <summary>
 /// Get container presssions
 /// </summary>
 /// <param name="container">A cloudblobcontainer object</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">Blob request option</param>
 /// <param name="operationContext">Operation context</param>
 /// <returns>The container's permission</returns>
 public BlobContainerPermissions GetContainerPermissions(CloudBlobContainer container, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return container.GetPermissions(accessCondition, options, operationContext);
 }
        private SharedAccessBlobPolicy GetNonExpiredContainerPolicy(CloudBlobContainer container)
        {
            SharedAccessBlobPolicy policy;
            bool policyExpired = false;
            var permissions = container.GetPermissions();
            var policyExists = permissions.SharedAccessPolicies.TryGetValue(PolicyName, out policy);

            if (policyExists)
            {
                policyExpired = policy.SharedAccessExpiryTime < DateTime.UtcNow;
            }

            if (policyExpired || !policyExists)
            {
                policy = CreateSharedAccessPolicy();
                permissions.SharedAccessPolicies.Remove(PolicyName);
                permissions.SharedAccessPolicies.Add(PolicyName, policy);
                container.SetPermissions(permissions);
            }

            return policy;
        }
Ejemplo n.º 11
0
 public static void PackContainerCompareData(CloudBlobContainer container, Dictionary<string, object> dic)
 {
     BlobContainerPermissions permissions = container.GetPermissions();
     dic["PublicAccess"] = permissions.PublicAccess;
     dic["Permission"] = permissions;
     dic["LastModified"] = container.Properties.LastModified;
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Test container reads and writes, expecting success.
 /// </summary>
 /// <param name="testContainer">The container.</param>
 /// <param name="testAccessCondition">The access condition to use.</param>
 private void ContainerReadWriteExpectLeaseSuccess(CloudBlobContainer testContainer, AccessCondition testAccessCondition)
 {
     testContainer.FetchAttributes(testAccessCondition, null /* options */);
     testContainer.GetPermissions(testAccessCondition, null /* options */);
     testContainer.SetMetadata(testAccessCondition, null /* options */);
     testContainer.SetPermissions(new BlobContainerPermissions(), testAccessCondition, null /* options */);
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Test container reads and writes, expecting lease failure.
        /// </summary>
        /// <param name="testContainer">The container.</param>
        /// <param name="testAccessCondition">The failing access condition to use.</param>
        /// <param name="expectedErrorCode">The expected error code.</param>
        /// <param name="description">The reason why these calls should fail.</param>
        private void ContainerReadWriteExpectLeaseFailure(CloudBlobContainer testContainer, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description)
        {
            // FetchAttributes is a HEAD request with no extended error info, so it returns with the generic ConditionFailed error code.
            TestHelper.ExpectedException(
                () => testContainer.FetchAttributes(testAccessCondition, null /* options */),
                description + "(Fetch Attributes)",
                HttpStatusCode.PreconditionFailed);

            TestHelper.ExpectedException(
                () => testContainer.GetPermissions(testAccessCondition, null /* options */),
                description + " (Get Permissions)",
                expectedStatusCode,
                expectedErrorCode);
            TestHelper.ExpectedException(
                () => testContainer.SetMetadata(testAccessCondition, null /* options */),
                description + " (Set Metadata)",
                expectedStatusCode,
                expectedErrorCode);
            TestHelper.ExpectedException(
                () => testContainer.SetPermissions(new BlobContainerPermissions(), testAccessCondition, null /* options */),
                description + " (Set Permissions)",
                expectedStatusCode,
                expectedErrorCode);
        }
Ejemplo n.º 14
0
        static void CreateSharedAccessPolicy(CloudBlobClient blobClient, CloudBlobContainer container, string policyName)
        {
            SharedAccessBlobPolicy sharedPolicy = new SharedAccessBlobPolicy()
            {
                SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
                                            Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read
            };

            BlobContainerPermissions permissions = container.GetPermissions();

            permissions.SharedAccessPolicies.Add(policyName, sharedPolicy);
            container.SetPermissions(permissions);


        }
Ejemplo n.º 15
0
        public CloudBlobContainer GetContainer(string containerName)
        {
            container = blobClient.GetContainerReference(containerName);

            if (container.CreateIfNotExists())
            {
                // configure container for public access
                var permissions = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);
            }

            return container;
        }