Esempio n. 1
0
        public void CloudBlobContainerCreate()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            container.Create();
            TestHelper.ExpectedException(
                () => container.Create(),
                "Creating already exists container should fail",
                HttpStatusCode.Conflict);
            container.Delete();
        }
Esempio n. 2
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();
        }
        private void PrefixEscapingTest(string prefix, string blobName)
        {
            CloudBlobClient    service   = GenerateCloudBlobClient();
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                string text = Guid.NewGuid().ToString();

                // Create from CloudBlobContainer.
                CloudBlockBlob originalBlob = container.GetBlockBlobReference(prefix + "/" + blobName);
                originalBlob.PutBlockList(new string[] { });

                // List blobs from container.
                IListBlobItem blobFromContainerListingBlobs = container.ListBlobs(null, true).First();
                Assert.AreEqual(originalBlob.Uri, blobFromContainerListingBlobs.Uri);

                CloudBlockBlob uriBlob = new CloudBlockBlob(originalBlob.Uri, service.Credentials);

                // Check Name
                Assert.AreEqual <string>(prefix + "/" + blobName, originalBlob.Name);
                Assert.AreEqual <string>(prefix + "/" + blobName, uriBlob.Name);

                // Absolute URI access from CloudBlockBlob
                CloudBlockBlob blobInfo = new CloudBlockBlob(originalBlob.Uri, service.Credentials);
                blobInfo.FetchAttributes();

                // Access from CloudBlobDirectory
                CloudBlobDirectory cloudBlobDirectory         = container.GetDirectoryReference(prefix);
                CloudBlockBlob     blobFromCloudBlobDirectory = cloudBlobDirectory.GetBlockBlobReference(blobName);
                Assert.AreEqual(blobInfo.Uri, blobFromCloudBlobDirectory.Uri);

                // Copy blob verification.
                CloudBlockBlob copyBlob = container.GetBlockBlobReference(prefix + "/" + blobName + "copy");
                copyBlob.StartCopyFromBlob(blobInfo.Uri);
                copyBlob.FetchAttributes();
            }
            finally
            {
                container.Delete();
            }
        }
Esempio n. 4
0
        public void BlobWriteStreamOpenAndClose()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                using (Stream blobStream = blockBlob.OpenWrite())
                {
                }

                CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("blob1");
                blockBlob2.FetchAttributes();
                Assert.AreEqual(0, blockBlob2.Properties.Length);
                Assert.AreEqual(BlobType.BlockBlob, blockBlob2.Properties.BlobType);

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                TestHelper.ExpectedException(
                    () => pageBlob.OpenWrite(null),
                    "Opening a page blob stream with no size should fail on a blob that does not exist",
                    HttpStatusCode.NotFound);
                using (Stream blobStream = pageBlob.OpenWrite(1024))
                {
                }
                using (Stream blobStream = pageBlob.OpenWrite(null))
                {
                }

                CloudPageBlob pageBlob2 = container.GetPageBlobReference("blob2");
                pageBlob2.FetchAttributes();
                Assert.AreEqual(1024, pageBlob2.Properties.Length);
                Assert.AreEqual(BlobType.PageBlob, pageBlob2.Properties.BlobType);
            }
            finally
            {
                container.Delete();
            }
        }
Esempio n. 5
0
        public void CloudBlobContainerCreateAPM()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            using (AutoResetEvent waitHandle = new AutoResetEvent(false))
            {
                IAsyncResult result = container.BeginCreate(
                    ar => waitHandle.Set(),
                    null);
                waitHandle.WaitOne();
                container.EndCreate(result);
                result = container.BeginCreate(
                    ar => waitHandle.Set(),
                    null);
                waitHandle.WaitOne();
                TestHelper.ExpectedException(
                    () => container.EndCreate(result),
                    "Creating already exists container should fail",
                    HttpStatusCode.Conflict);
            }
            container.Delete();
        }
 /// <summary>
 /// Delete container
 /// </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>
 public void DeleteContainer(CloudBlobContainer container, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     container.Delete(accessCondition, options, operationContext);
 }
Esempio n. 7
0
 /// <summary>
 /// Test container deletion, expecting success.
 /// </summary>
 /// <param name="testContainer">The container.</param>
 /// <param name="testAccessCondition">The access condition to use.</param>
 private void ContainerDeleteExpectLeaseSuccess(CloudBlobContainer testContainer, AccessCondition testAccessCondition)
 {
     testContainer.Delete(testAccessCondition, null /* options */);
 }
Esempio n. 8
0
 /// <summary>
 /// Test container deletion, 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 ContainerDeleteExpectLeaseFailure(CloudBlobContainer testContainer, AccessCondition testAccessCondition, HttpStatusCode expectedStatusCode, string expectedErrorCode, string description)
 {
     TestHelper.ExpectedException(
         () => testContainer.Delete(testAccessCondition, null /* options */),
         description + " (Delete)",
         expectedStatusCode,
         expectedErrorCode);
 }
Esempio n. 9
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));
            }
        }