Exemple #1
0
        public static bool RemoveDirectoryLayers(string containerName, string directoryName)
        {
            var client    = StorageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName.ToLower());

            if (!container.Exists())
            {
                return(false);
            }

            var directory = container.GetDirectoryReference(directoryName.Substring(0));

            CloudBlobDirectory dira = container.GetDirectoryReference("images");
            CloudBlobDirectory dirb = dira.GetSubdirectoryReference("layers");
            CloudBlobDirectory dirc = dirb.GetSubdirectoryReference(directoryName);

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

            foreach (var blob in blobs)
            {
                if (blob is ICloudBlob)
                {
                    ((ICloudBlob)blob).DeleteIfExists();
                }
                else if (blob is CloudBlobDirectory)
                {
                    var dird     = blob as CloudBlobDirectory;
                    var subBlobs = dird.ListBlobs().ToList();
                    foreach (var subblob in subBlobs)
                    {
                        var cloudBlob = subblob as ICloudBlob;
                        if (cloudBlob != null)
                        {
                            cloudBlob.DeleteIfExists();
                        }
                    }
                }
            }



            return(true);
        }
        public void BlobTypesWithStorageUri()
        {
            StorageUri endpoint = new StorageUri(
                new Uri("http://" + AccountName + BlobService + EndpointSuffix),
                new Uri("http://" + AccountName + SecondarySuffix + BlobService + EndpointSuffix));

            CloudBlobClient client = new CloudBlobClient(endpoint, new StorageCredentials());

            Assert.IsTrue(endpoint.Equals(client.StorageUri));
            Assert.IsTrue(endpoint.PrimaryUri.Equals(client.BaseUri));

            StorageUri containerUri = new StorageUri(
                new Uri(endpoint.PrimaryUri + "container"),
                new Uri(endpoint.SecondaryUri + "container"));

            CloudBlobContainer container = client.GetContainerReference("container");

            Assert.IsTrue(containerUri.Equals(container.StorageUri));
            Assert.IsTrue(containerUri.PrimaryUri.Equals(container.Uri));
            Assert.IsTrue(endpoint.Equals(container.ServiceClient.StorageUri));

            container = new CloudBlobContainer(containerUri, client.Credentials);
            Assert.IsTrue(containerUri.Equals(container.StorageUri));
            Assert.IsTrue(containerUri.PrimaryUri.Equals(container.Uri));
            Assert.IsTrue(endpoint.Equals(container.ServiceClient.StorageUri));

            StorageUri directoryUri = new StorageUri(
                new Uri(containerUri.PrimaryUri + "/directory/"),
                new Uri(containerUri.SecondaryUri + "/directory/"));

            StorageUri subdirectoryUri = new StorageUri(
                new Uri(directoryUri.PrimaryUri + "subdirectory/"),
                new Uri(directoryUri.SecondaryUri + "subdirectory/"));

            CloudBlobDirectory directory = container.GetDirectoryReference("directory");

            Assert.IsTrue(directoryUri.Equals(directory.StorageUri));
            Assert.IsTrue(directoryUri.PrimaryUri.Equals(directory.Uri));
            Assert.IsNotNull(directory.Parent);
            Assert.IsTrue(containerUri.Equals(directory.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(directory.ServiceClient.StorageUri));

            CloudBlobDirectory subdirectory = directory.GetSubdirectoryReference("subdirectory");

            Assert.IsTrue(subdirectoryUri.Equals(subdirectory.StorageUri));
            Assert.IsTrue(subdirectoryUri.PrimaryUri.Equals(subdirectory.Uri));
            Assert.IsTrue(directoryUri.Equals(subdirectory.Parent.StorageUri));
            Assert.IsTrue(containerUri.Equals(subdirectory.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(subdirectory.ServiceClient.StorageUri));

            StorageUri blobUri = new StorageUri(
                new Uri(subdirectoryUri.PrimaryUri + "blob"),
                new Uri(subdirectoryUri.SecondaryUri + "blob"));

            CloudBlockBlob blockBlob = subdirectory.GetBlockBlobReference("blob");

            Assert.IsTrue(blobUri.Equals(blockBlob.StorageUri));
            Assert.IsTrue(blobUri.PrimaryUri.Equals(blockBlob.Uri));
            Assert.IsTrue(subdirectoryUri.Equals(blockBlob.Parent.StorageUri));
            Assert.IsTrue(containerUri.Equals(blockBlob.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(blockBlob.ServiceClient.StorageUri));

            blockBlob = new CloudBlockBlob(blobUri, null, client.Credentials);
            Assert.IsTrue(blobUri.Equals(blockBlob.StorageUri));
            Assert.IsTrue(blobUri.PrimaryUri.Equals(blockBlob.Uri));
            Assert.IsTrue(subdirectoryUri.Equals(blockBlob.Parent.StorageUri));
            Assert.IsTrue(containerUri.Equals(blockBlob.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(blockBlob.ServiceClient.StorageUri));

            CloudPageBlob pageBlob = subdirectory.GetPageBlobReference("blob");

            Assert.IsTrue(blobUri.Equals(pageBlob.StorageUri));
            Assert.IsTrue(blobUri.PrimaryUri.Equals(pageBlob.Uri));
            Assert.IsTrue(subdirectoryUri.Equals(pageBlob.Parent.StorageUri));
            Assert.IsTrue(containerUri.Equals(pageBlob.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(pageBlob.ServiceClient.StorageUri));

            pageBlob = new CloudPageBlob(blobUri, null, client.Credentials);
            Assert.IsTrue(blobUri.Equals(pageBlob.StorageUri));
            Assert.IsTrue(blobUri.PrimaryUri.Equals(pageBlob.Uri));
            Assert.IsTrue(subdirectoryUri.Equals(pageBlob.Parent.StorageUri));
            Assert.IsTrue(containerUri.Equals(pageBlob.Container.StorageUri));
            Assert.IsTrue(endpoint.Equals(pageBlob.ServiceClient.StorageUri));
        }