示例#1
0
        public void CloudBlobContainerSetMetadata()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                container2.FetchAttributes();
                Assert.AreEqual(0, container2.Metadata.Count);

                container.Metadata.Add("key1", "value1");
                container.SetMetadata();

                container2.FetchAttributes();
                Assert.AreEqual(1, container2.Metadata.Count);
                Assert.AreEqual("value1", container2.Metadata["key1"]);

                CloudBlobContainer container3 = container.ServiceClient.ListContainers(container.Name, ContainerListingDetails.Metadata).First();
                Assert.AreEqual(1, container3.Metadata.Count);
                Assert.AreEqual("value1", container3.Metadata["key1"]);

                container.Metadata.Clear();
                container.SetMetadata();

                container2.FetchAttributes();
                Assert.AreEqual(0, container2.Metadata.Count);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
示例#2
0
        public void CloudBlobContainerConditionalAccess()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                container.FetchAttributes();

                string         currentETag         = container.Properties.ETag;
                DateTimeOffset currentModifiedTime = container.Properties.LastModified.Value;

                // ETag conditional tests
                container.Metadata["ETagConditionalName"] = "ETagConditionalValue";
                container.SetMetadata();

                container.FetchAttributes();
                string newETag = container.Properties.ETag;
                Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata");

                // LastModifiedTime tests
                currentModifiedTime = container.Properties.LastModified.Value;

                container.Metadata["DateConditionalName"] = "DateConditionalValue";

                TestHelper.ExpectedException(
                    () => container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(currentModifiedTime), null),
                    "IfModifiedSince conditional on current modified time should throw",
                    HttpStatusCode.PreconditionFailed,
                    "ConditionNotMet");

                container.Metadata["DateConditionalName"] = "DateConditionalValue2";
                currentETag = container.Properties.ETag;

                DateTimeOffset pastTime = currentModifiedTime.Subtract(TimeSpan.FromMinutes(5));
                container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null);

                pastTime = currentModifiedTime.Subtract(TimeSpan.FromHours(5));
                container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null);

                pastTime = currentModifiedTime.Subtract(TimeSpan.FromDays(5));
                container.SetMetadata(AccessCondition.GenerateIfModifiedSinceCondition(pastTime), null);

                container.FetchAttributes();
                newETag = container.Properties.ETag;
                Assert.AreNotEqual(newETag, currentETag, "ETage should be modified on write metadata");
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
示例#3
0
        /// <summary>
        /// Occurs when a storage provider operation has completed.
        /// </summary>
        //public event EventHandler<StorageProviderEventArgs> StorageProviderOperationCompleted;

        #endregion

        // Initialiser method
        private void Initialise(string storageAccount, string containerName)
        {
            if (String.IsNullOrEmpty(containerName))
                throw new ArgumentException("You must provide the base Container Name", "containerName");
            
            ContainerName = containerName;

            _account = CloudStorageAccount.Parse(storageAccount);
            _blobClient = _account.CreateCloudBlobClient();
            _container = _blobClient.GetContainerReference(ContainerName);
            try
            {
                _container.FetchAttributes();
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information");
                _container.Create();

                // set new container's permissions
                // Create a permission policy to set the public access setting for the container. 
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // The public access setting explicitly specifies that the container is private,
                // so that it can't be accessed anonymously.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

                //Set the permission policy on the container.
                _container.SetPermissions(containerPermissions);
            }
        }
        /// <summary>
        /// create a container with random properties and metadata
        /// </summary>
        /// <param name="containerName">container name</param>
        /// <returns>the created container object with properties and metadata</returns>
        public StorageBlob.CloudBlobContainer CreateContainer(string containerName = "")
        {
            if (String.IsNullOrEmpty(containerName))
            {
                containerName = Utility.GenNameString("container");
            }

            StorageBlob.CloudBlobContainer container = client.GetContainerReference(containerName);
            container.CreateIfNotExists();

            //there is no properties to set
            container.FetchAttributes();

            int minMetaCount       = 1;
            int maxMetaCount       = 5;
            int minMetaValueLength = 10;
            int maxMetaValueLength = 20;
            int count = random.Next(minMetaCount, maxMetaCount);

            for (int i = 0; i < count; i++)
            {
                string metaKey     = Utility.GenNameString("metatest");
                int    valueLength = random.Next(minMetaValueLength, maxMetaValueLength);
                string metaValue   = Utility.GenNameString("metavalue-", valueLength);
                container.Metadata.Add(metaKey, metaValue);
            }

            container.SetMetadata();

            Test.Info(string.Format("create container '{0}'", containerName));
            return(container);
        }
示例#5
0
        internal void GetContainerTest(Agent agent)
        {
            string NEW_CONTAINER_NAME = Utility.GenNameString("astoria-");

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

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

            Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> > {
                dic
            };

            try
            {
                //--------------Get operation--------------
                Test.Assert(agent.GetAzureStorageContainer(NEW_CONTAINER_NAME), Utility.GenComparisonData("GetAzureStorageContainer", true));
                // Verification for returned values
                container.FetchAttributes();
                dic.Add("CloudBlobContainer", container);
                CloudBlobUtil.PackContainerCompareData(container, dic);

                agent.OutputValidation(comp);
            }
            finally
            {
                // clean up
                container.DeleteIfExists();
            }
        }
        private static void TestAccess(BlobContainerPublicAccessType accessType, CloudBlobContainer container, CloudBlob inputBlob)
        {
            StorageCredentials credentials = new StorageCredentials();
            container = new CloudBlobContainer(container.Uri, credentials);
            CloudPageBlob blob = new CloudPageBlob(inputBlob.Uri, credentials);

            if (accessType.Equals(BlobContainerPublicAccessType.Container))
            {
                blob.FetchAttributes();
                container.ListBlobs().ToArray();
                container.FetchAttributes();
            }
            else if (accessType.Equals(BlobContainerPublicAccessType.Blob))
            {
                blob.FetchAttributes();
                TestHelper.ExpectedException(
                    () => container.ListBlobs().ToArray(),
                    "List blobs while public access does not allow for listing",
                    HttpStatusCode.NotFound);
                TestHelper.ExpectedException(
                    () => container.FetchAttributes(),
                    "Fetch container attributes while public access does not allow",
                    HttpStatusCode.NotFound);
            }
            else
            {
                TestHelper.ExpectedException(
                    () => blob.FetchAttributes(),
                    "Fetch blob attributes while public access does not allow",
                    HttpStatusCode.NotFound);
                TestHelper.ExpectedException(
                    () => container.ListBlobs().ToArray(),
                    "List blobs while public access does not allow for listing",
                    HttpStatusCode.NotFound);
                TestHelper.ExpectedException(
                    () => container.FetchAttributes(),
                    "Fetch container attributes while public access does not allow",
                    HttpStatusCode.NotFound);
            }
        }
示例#7
0
        public static void ListContainerMetadata(CloudBlobContainer container)
        {
            //Fetch container attributes in order to populate the container's properties and metadata.
            container.FetchAttributes();

            //Enumerate the container's metadata.

            //Console.WriteLine("Container metadata:");
            //foreach (var metadataItem in container.Metadata)
            //{
            //    Console.WriteLine("\tKey: {0}", metadataItem.Key);
            //    Console.WriteLine("\tValue: {0}", metadataItem.Value);
            //}
        }
示例#8
0
        /// <summary>
        /// This method returns true if specified container exists
        /// But if container doesn't exists return CLOUDCONTAINERNOTFOUNDEXCEPTION
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static Boolean isContainerExists(Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer container)
        {
            try {
                container.FetchAttributes();
                return(true);
            }
            catch (StorageException ex) {
                Console.WriteLine(ex);
                return(false);

                /*
                 * if (ex.ErrorCode == StorageErrorCode.ResourceNotFound)
                 * {
                 *  return false;
                 * }
                 * else {
                 *  throw;
                 * }*/
            }
        }
示例#9
0
        public void CloudBlobContainerCreateWithMetadata()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Metadata.Add("key1", "value1");
                container.Create();

                CloudBlobContainer container2 = container.ServiceClient.GetContainerReference(container.Name);
                container2.FetchAttributes();
                Assert.AreEqual(1, container2.Metadata.Count);
                Assert.AreEqual("value1", container2.Metadata["key1"]);

                Assert.IsTrue(container2.Properties.LastModified.Value.AddHours(1) > DateTimeOffset.Now);
                Assert.IsNotNull(container2.Properties.ETag);
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
 /// <summary>
 /// Fetch container attributes
 /// </summary>
 /// <param name="container">CloudBlobContainer object</param>
 /// <param name="accessCondition">Access condition</param>
 /// <param name="options">Blob request options</param>
 /// <param name="operationContext">An object that represents the context for the current operation.</param>
 public void FetchContainerAttributes(CloudBlobContainer container, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     container.FetchAttributes(accessCondition, options, operationContext);
 }
        /// <summary>
        /// Gets the latest BLOB.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns></returns>
        private CloudBlockBlob GetLatestBlob(CloudBlobContainer container)
        {
            container.FetchAttributes();
            var latest = container.Metadata[Constants.LastUploadedVersion];

            return container.GetBlockBlobReference(latest);
        }
示例#12
0
        /// <summary>
        /// Checks the lease status of a container, both from its attributes and from a container listing.
        /// </summary>
        /// <param name="container">The container to test.</param>
        /// <param name="expectedStatus">The expected lease status.</param>
        /// <param name="expectedState">The expected lease state.</param>
        /// <param name="expectedDuration">The expected lease duration.</param>
        /// <param name="description">A description of the circumstances that lead to the expected status.</param>
        private void CheckLeaseStatus(
            CloudBlobContainer container,
            LeaseStatus expectedStatus,
            LeaseState expectedState,
            LeaseDuration expectedDuration,
            string description)
        {
            container.FetchAttributes();
            Assert.AreEqual(expectedStatus, container.Properties.LeaseStatus, "LeaseStatus mismatch: " + description + " (from FetchAttributes)");
            Assert.AreEqual(expectedState, container.Properties.LeaseState, "LeaseState mismatch: " + description + " (from FetchAttributes)");
            Assert.AreEqual(expectedDuration, container.Properties.LeaseDuration, "LeaseDuration mismatch: " + description + " (from FetchAttributes)");

            BlobContainerProperties propertiesInListing = (from CloudBlobContainer c in this.blobClient.ListContainers(
                                                               container.Name,
                                                               ContainerListingDetails.None)
                                                           where c.Name == container.Name
                                                           select c.Properties).Single();

            Assert.AreEqual(expectedStatus, propertiesInListing.LeaseStatus, "LeaseStatus mismatch: " + description + " (from ListContainers)");
            Assert.AreEqual(expectedState, propertiesInListing.LeaseState, "LeaseState mismatch: " + description + " (from ListContainers)");
            Assert.AreEqual(expectedDuration, propertiesInListing.LeaseDuration, "LeaseDuration mismatch: " + description + " (from ListContainers)");
        }
示例#13
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 */);
 }
示例#14
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);
        }
示例#15
0
        /// <summary>
        /// Verifies the behavior of a lease while the lease holds. Once the lease expires, this method confirms that write operations succeed.
        /// The test is cut short once the <c>testLength</c> time has elapsed.
        /// </summary>
        /// <param name="leasedContainer">The container.</param>
        /// <param name="duration">The duration of the lease.</param>
        /// <param name="testLength">The maximum length of time to run the test.</param>
        /// <param name="tolerance">The allowed lease time error.</param>
        internal void ContainerAcquireRenewLeaseTest(CloudBlobContainer leasedContainer, TimeSpan? duration, TimeSpan testLength, TimeSpan tolerance)
        {
            DateTime beginTime = DateTime.UtcNow;

            while (true)
            {
                try
                {
                    // Attempt to delete the container with no lease ID.
                    leasedContainer.Delete();

                    // The delete succeeded, which means that the lease must have expired.

                    // If the lease was infinite then there is an error because it should not have expired.
                    Assert.IsNotNull(duration, "An infinite lease should not expire.");

                    // The lease should be past its expiration time.
                    Assert.IsTrue(DateTime.UtcNow - beginTime > duration - tolerance, "Deletes should not succeed while lease is present.");

                    // Since the lease has expired (and the container was deleted), the test is over.
                    return;
                }
                catch (StorageException exception)
                {
                    if (exception.RequestInformation.ExtendedErrorInformation.ErrorCode == BlobErrorCodeStrings.LeaseIdMissing)
                    {
                        // We got this error because the lease has not expired yet.

                        // Make sure the lease is not past its expiration time yet.
                        DateTime currentTime = DateTime.UtcNow;
                        if (duration.HasValue)
                        {
                            Assert.IsTrue(currentTime - beginTime < duration + tolerance, "Deletes should succeed after a lease expires.");
                        }

                        // End the test early if necessary
                        if (currentTime - beginTime > testLength)
                        {
                            // The lease has not expired, but we're not waiting any longer.
                            return;
                        }
                    }
                    else
                    {
                        throw;
                    }
                }

                // Attempt to write to and read from the container. This should always succeed.
                leasedContainer.SetMetadata();
                leasedContainer.FetchAttributes();

                // Wait 1 second before trying again.
                Thread.Sleep(TimeSpan.FromSeconds(1));
            }
        }
        // Initialiser method
        private void Initialise(string containerName)
        {
            if (String.IsNullOrEmpty(containerName))
                throw new ArgumentException("You must provide the base Container Name", "containerName");

            ContainerName = containerName;

            if (StorageProviderConfiguration.Mode == Modes.Debug)
            {
                _account = CloudStorageAccount.DevelopmentStorageAccount;
                _blobClient = _account.CreateCloudBlobClient();
                _blobClient.ServerTimeout = new TimeSpan(0, 0, 0, 5);
            }
            else
            {
                _account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("FTP2Azure.StorageAccount"));
                _blobClient = _account.CreateCloudBlobClient();
                _blobClient.ServerTimeout = new TimeSpan(0, 0, 0, 5);
            }

            _container = _blobClient.GetContainerReference(ContainerName);
            try
            {
                _container.FetchAttributes();
            }
            catch (StorageException)
            {
                Trace.WriteLine(string.Format("Create new container: {0}", ContainerName), "Information");
                _container.Create();

                // set new container's permissions
                // Create a permission policy to set the public access setting for the container.
                BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

                // The public access setting explicitly specifies that the container is private,
                // so that it can't be accessed anonymously.
                containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

                //Set the permission policy on the container.
                _container.SetPermissions(containerPermissions);
            }
        }