static void Main(string[] args) { storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString); blobStorage = storageAccount.CreateCloudBlobClient(); accessibleContainers = ConfigurationManager.AppSettings["Containers"].Split(new char[] { '|' }); var containers = blobStorage.ListContainers().Where(con => accessibleContainers.Contains(con.Name)); foreach (var container in containers) { foreach (IListBlobItem blobRef in container.ListBlobs(useFlatBlobListing: true)) { CloudBlockBlob blob = container.GetBlockBlobReference(blobRef.Uri.ToString()); blob.FetchAttributes(); if (!ContentTypes.Values.Contains(blob.Properties.ContentType)) { blob.Properties.ContentType = GetContentType(GetExtension(blobRef.Uri.ToString())); blob.SetProperties(); } } } }
public void CloudBlobClientListContainers() { string name = GetRandomContainerName(); List <string> containerNames = new List <string>(); CloudBlobClient blobClient = GenerateCloudBlobClient(); for (int i = 0; i < 3; i++) { string containerName = name + i.ToString(); containerNames.Add(containerName); blobClient.GetContainerReference(containerName).Create(); } IEnumerable <CloudBlobContainer> results = blobClient.ListContainers(); foreach (CloudBlobContainer container in results) { if (containerNames.Remove(container.Name)) { container.Delete(); } } Assert.AreEqual(0, containerNames.Count); }
/// <summary> /// Deletes containers starting with specified prefix. /// Note that the ListContainers method is called synchronously, for the purposes of the sample. However, in a real-world /// application using the async/await pattern, best practices recommend using asynchronous methods consistently. /// </summary> /// <param name="blobClient">The Blob service client.</param> /// <param name="prefix">The container name prefix.</param> /// <returns>A Task object.</returns> private static async Task DeleteContainersWithPrefixAsync(CloudBlobClient blobClient, string prefix) { Console.WriteLine("Delete all containers beginning with the specified prefix"); try { foreach (var container in blobClient.ListContainers(prefix)) { Console.WriteLine("\tContainer:" + container.Name); if (container.Properties.LeaseState == LeaseState.Leased) { await container.BreakLeaseAsync(null); } await container.DeleteAsync(); } Console.WriteLine(); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
/// <summary> /// Demonstrates container lease states: available, breaking, broken, and expired. /// A lease is used in each example to delete the container. /// </summary> /// <param name="blobClient">The Blob service client.</param> /// <returns>A Task object.</returns> private static async Task ManageContainerLeasesAsync(CloudBlobClient blobClient) { CloudBlobContainer container1 = null; CloudBlobContainer container2 = null; CloudBlobContainer container3 = null; CloudBlobContainer container4 = null; CloudBlobContainer container5 = null; // Lease duration is 15 seconds. TimeSpan leaseDuration = new TimeSpan(0, 0, 15); const string LeasingPrefix = "leasing-"; try { string leaseId = null; AccessCondition condition = null; /* Case 1: Lease is available */ // Lease is available on the new container. Acquire the lease and delete the leased container. container1 = blobClient.GetContainerReference(LeasingPrefix + Guid.NewGuid()); await container1.CreateIfNotExistsAsync(); // Get container properties to see the available lease. await container1.FetchAttributesAsync(); PrintContainerLeaseProperties(container1); // Acquire the lease. leaseId = await container1.AcquireLeaseAsync(leaseDuration, leaseId); // Get container properties again to see that the container is leased. await container1.FetchAttributesAsync(); PrintContainerLeaseProperties(container1); // Create an access condition using the lease ID, and use it to delete the leased container.. condition = new AccessCondition() { LeaseId = leaseId }; await container1.DeleteAsync(condition, null, null); Console.WriteLine("Deleted container {0}", container1.Name); /* Case 2: Lease is breaking */ container2 = blobClient.GetContainerReference(LeasingPrefix + Guid.NewGuid()); await container2.CreateIfNotExistsAsync(); // Acquire the lease. leaseId = await container2.AcquireLeaseAsync(leaseDuration, null); // Break the lease. Passing null indicates that the break interval will be the remainder of the current lease. await container2.BreakLeaseAsync(null); // Get container properties to see the breaking lease. // The lease break interval has not yet elapsed. await container2.FetchAttributesAsync(); PrintContainerLeaseProperties(container2); // Delete the container. If the lease is breaking, the container can be deleted by // passing the lease ID. condition = new AccessCondition() { LeaseId = leaseId }; await container2.DeleteAsync(condition, null, null); Console.WriteLine("Deleted container {0}", container2.Name); /* Case 3: Lease is broken */ container3 = blobClient.GetContainerReference(LeasingPrefix + Guid.NewGuid()); await container3.CreateIfNotExistsAsync(); // Acquire the lease. leaseId = await container3.AcquireLeaseAsync(leaseDuration, null); // Break the lease. Passing 0 breaks the lease immediately. TimeSpan breakInterval = await container3.BreakLeaseAsync(new TimeSpan(0)); // Get container properties to see that the lease is broken. await container3.FetchAttributesAsync(); PrintContainerLeaseProperties(container3); // Once the lease is broken, delete the container without the lease ID. await container3.DeleteAsync(); Console.WriteLine("Deleted container {0}", container3.Name); /* Case 4: Lease has expired. */ container4 = blobClient.GetContainerReference(LeasingPrefix + Guid.NewGuid()); await container4.CreateIfNotExistsAsync(); // Acquire the lease. leaseId = await container4.AcquireLeaseAsync(leaseDuration, null); // Sleep for 16 seconds to allow lease to expire. Console.WriteLine("Waiting 16 seconds for lease break interval to expire...."); System.Threading.Thread.Sleep(new TimeSpan(0, 0, 16)); // Get container properties to see that the lease has expired. await container4.FetchAttributesAsync(); PrintContainerLeaseProperties(container4); // Delete the container without the lease ID. await container4.DeleteAsync(); /* Case 5: Attempt to delete leased container without lease ID. */ container5 = blobClient.GetContainerReference(LeasingPrefix + Guid.NewGuid()); await container5.CreateIfNotExistsAsync(); // Acquire the lease. await container5.AcquireLeaseAsync(leaseDuration, null); // Get container properties to see that the container has been leased. await container5.FetchAttributesAsync(); PrintContainerLeaseProperties(container5); // Attempt to delete the leased container without the lease ID. // This operation will result in an error. // Note that in a real-world scenario, it would most likely be another client attempting to delete the container. await container5.DeleteAsync(); } catch (StorageException e) { if (e.RequestInformation.HttpStatusCode == 412) { // Handle the error demonstrated for case 5 above and continue execution. Console.WriteLine("The container is leased and cannot be deleted without specifying the lease ID."); Console.WriteLine("More information: {0}", e.Message); } else { // Output error information for any other errors, but continue execution. Console.WriteLine(e.Message); } } finally { // Enumerate containers based on the prefix used to name them, and delete any remaining containers. foreach (var container in blobClient.ListContainers(LeasingPrefix)) { await container.FetchAttributesAsync(); if (container.Properties.LeaseState == LeaseState.Leased || container.Properties.LeaseState == LeaseState.Breaking) { await container.BreakLeaseAsync(new TimeSpan(0)); } Console.WriteLine(); Console.WriteLine("Deleting container: {0}", container.Name); await container.DeleteAsync(); } } }
/// <summary> /// Lists all containers in the storage account. /// Note that the ListContainers method is called synchronously, for the purposes of the sample. However, in a real-world /// application using the async/await pattern, best practices recommend using asynchronous methods consistently. /// </summary> /// <param name="blobClient">The Blob service client.</param> /// <param name="prefix">The container prefix.</param> private static void ListAllContainers(CloudBlobClient blobClient, string prefix) { // List all containers in this storage account. Console.WriteLine("List all containers in account:"); try { // List containers beginning with the specified prefix, and without returning container metadata. foreach (var container in blobClient.ListContainers(prefix, ContainerListingDetails.None, null, null)) { Console.WriteLine("\tContainer:" + container.Name); } Console.WriteLine(); } catch (StorageException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } }
/// <summary> /// list all the existing containers /// </summary> /// <returns>a list of cloudblobcontainer object</returns> public List <StorageBlob.CloudBlobContainer> GetExistingContainers() { StorageBlob.ContainerListingDetails details = StorageBlob.ContainerListingDetails.All; return(client.ListContainers(string.Empty, details).ToList()); }
private void ConnectToStorage() { try { if (radioButtonStorageDefault.Checked) { storageAccount = new CloudStorageAccount(new StorageCredentials(contextUpload.DefaultStorageAccount.Name, MediaServicesStorageAccountKey), mystoragesuffix, true); } else { storageAccount = new CloudStorageAccount(new StorageCredentials(textBoxStorageName.Text, textBoxStorageKey.Text), mystoragesuffix, true); } } catch { MessageBox.Show("There is a problem when connecting to the storage account"); ErrorConnect = true; return; } cloudBlobClient = storageAccount.CreateCloudBlobClient(); mediaBlobContainers = cloudBlobClient.ListContainers(); ErrorConnect = false; }
//****************** //* * //* LoadLeftPane * //* * //****************** // Load a list of storage containers/queues/tables into the left pane of the storage view. public void LoadLeftPane() { Cursor = Cursors.Wait; NewAction(); AccountTitle.Text = Account.Name; ClearMainPane(); TreeViewItem blobSection = new TreeViewItem() { Header = "Blob Containers", Tag = new OutlineItem() { ItemType = ItemType.BLOB_SERVICE /* 100 */, Container = null } }; TreeViewItem queueSection = new TreeViewItem() { Header = "Queues", Tag = new OutlineItem() { ItemType = ItemType.QUEUE_SERVICE /* 200 */, Container = null } }; TreeViewItem tableSection = new TreeViewItem() { Header = "Tables", Tag = new OutlineItem() { ItemType = ItemType.TABLE_SERVICE /* 300 */, Container = null } }; AccountTreeView.Items.Clear(); AccountTreeView.Items.Add(blobSection); AccountTreeView.Items.Add(queueSection); AccountTreeView.Items.Add(tableSection); CloudStorageAccount account = OpenStorageAccount(); blobClient = account.CreateCloudBlobClient(); tableClient = account.CreateCloudTableClient(); queueClient = account.CreateCloudQueueClient(); try { var serviceProperties = blobClient.GetServiceProperties(); if (serviceProperties.Cors.CorsRules.Count == 0) { ButtonBlobServiceCORSIcon.Source = new BitmapImage(new Uri("pack://application:,,/Images/unchecked.png")); ButtonBlobServiceCORSLabel.Text = "CORS"; } else { ButtonBlobServiceCORSIcon.Source = new BitmapImage(new Uri("pack://application:,,/Images/checked.png")); ButtonBlobServiceCORSLabel.Text = "CORS (" + serviceProperties.Cors.CorsRules.Count.ToString() + ")"; } } catch(Exception) { // Disallowed for developer storage account. } try { // Check for $logs container and add it if present ($logs is not included in the general ListContainers call). CloudBlobContainer logsContainer = blobClient.GetContainerReference("$logs"); if (logsContainer.Exists()) { StackPanel stack = new StackPanel(); stack.Orientation = Orientation.Horizontal; Image cloudFolderImage = new Image(); cloudFolderImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/cloud_folder.png")); cloudFolderImage.Height = 24; Label label = new Label(); label.Content = logsContainer.Name; stack.Children.Add(cloudFolderImage); stack.Children.Add(label); TreeViewItem blobItem = new TreeViewItem() { Header = stack, Tag = new OutlineItem() { ItemType = ItemType.BLOB_CONTAINER, Container = logsContainer.Name, Permissions = logsContainer.GetPermissions() } }; blobSection.Items.Add(blobItem); } IEnumerable<CloudBlobContainer> containers = blobClient.ListContainers(); if (containers != null) { if (containers != null) { foreach (CloudBlobContainer container in containers) { StackPanel stack = new StackPanel(); stack.Orientation = Orientation.Horizontal; Image cloudFolderImage = new Image(); cloudFolderImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/cloud_folder.png")); cloudFolderImage.Height = 24; Label label = new Label(); label.Content = container.Name; stack.Children.Add(cloudFolderImage); stack.Children.Add(label); TreeViewItem blobItem = new TreeViewItem() { Header = stack, Tag = new OutlineItem() { ItemType = ItemType.BLOB_CONTAINER, Container = container.Name, Permissions = container.GetPermissions() } }; blobSection.Items.Add(blobItem); } } } blobSection.Header = "Blob Containers (" + containers.Count().ToString() + ")"; switch(LastItemType) { case ItemType.BLOB_SERVICE: case ItemType.BLOB_CONTAINER: blobSection.IsExpanded = true; break; case ItemType.QUEUE_SERVICE: case ItemType.QUEUE_CONTAINER: queueSection.IsExpanded = true; break; case ItemType.TABLE_SERVICE: case ItemType.TABLE_CONTAINER: tableSection.IsExpanded = true; break; default: blobSection.IsExpanded = true; break; } } catch (Exception ex) { ShowError("Error enumering blob containers in the storage account: " + ex.Message); } try { IEnumerable<CloudQueue> queues = queueClient.ListQueues(); if (queues != null) { foreach (CloudQueue queue in queues) { StackPanel stack = new StackPanel(); stack.Orientation = Orientation.Horizontal; Image cloudFolderImage = new Image(); cloudFolderImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/cloud_queue.png")); cloudFolderImage.Height = 24; Label label = new Label(); label.Content = queue.Name; stack.Children.Add(cloudFolderImage); stack.Children.Add(label); queueSection.Items.Add(new TreeViewItem() { Header = stack, Tag = new OutlineItem() { ItemType = ItemType.QUEUE_CONTAINER, Container = queue.Name } }); } } queueSection.Header = "Queues (" + queues.Count().ToString() + ")"; } catch (Exception ex) { ShowError("Error enumering queues in storage account: " + ex.Message); } // OData version number occurs here: // Could not load file or assembly 'Microsoft.Data.OData, Version=5.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. try { IEnumerable<CloudTable> tables = tableClient.ListTables(); if (tables != null) { foreach (CloudTable table in tables) { StackPanel stack = new StackPanel(); stack.Orientation = Orientation.Horizontal; Image cloudFolderImage = new Image(); cloudFolderImage.Source = new BitmapImage(new Uri("pack://application:,,/Images/cloud_table.png")); cloudFolderImage.Height = 24; Label label = new Label(); label.Content = table.Name; stack.Children.Add(cloudFolderImage); stack.Children.Add(label); tableSection.Items.Add(new TreeViewItem() { Header = stack, Tag = new OutlineItem() { ItemType = ItemType.TABLE_CONTAINER, Container = table.Name } }); } } tableSection.Header = "Tables (" + tables.Count().ToString() + ")"; } catch(Exception ex) { ShowError("Error enumerating tables in storage account: " + ex.Message); } Cursor = Cursors.Arrow; }
void CleanupImportClient(CloudBlobClient importClient, string containerName) { _runner.ExecuteRequest("http://mydashserver/container/" + containerName + "?restype=container", "DELETE", (HttpContent)null, HttpStatusCode.Accepted); foreach (var container in importClient.ListContainers()) { container.Delete(); } }
static void InitializeImportClient(CloudBlobClient importClient) { // Remove all existing containers - note that this is a race condition with others executing the tests concurrently, // but this is unlikely enough to avoid contention // Use a wait to try to avoid contention if (importClient.ListContainers().Any()) { Thread.Sleep(30000); } bool deletedContainers = false; foreach (var container in importClient.ListContainers()) { container.Delete(); deletedContainers = true; } // If we deleted any containers that will be recreated when the account is imported, we should // wait a while to allow XStore to get consistent around the deleted container if (deletedContainers) { Thread.Sleep(60000); } }
static async Task<IDictionary<string, CloudBlobContainer>> ListContainersAsync(CloudBlobClient client) { return (await Task.Factory.StartNew(() => client.ListContainers(null, ContainerListingDetails.All, null, null))) .ToDictionary(container => container.Name, StringComparer.OrdinalIgnoreCase); }
public void Load(string connectionString, Action<StorageAccount> onLoaded) { account = CloudStorageAccount.Parse(connectionString); client = account.CreateCloudBlobClient(); Containers.Clear(); Task.Run(() => client.ListContainers()) .ContinueWith(r => { if (!r.IsCompleted) return; Containers.AddRange(r.Result); onLoaded(this); }); }
private void Copy(CloudBlobClient srcBlobClient, CloudBlobClient dstBlobClient) { if (!Service_BlobSync.blobsSync) { return; } foreach (var srcCloudBlobContainer in srcBlobClient.ListContainers()) { if (srcCloudBlobContainer.Name != "vhds") { var dstCloudBlobContainer = dstBlobClient.GetContainerReference(srcCloudBlobContainer.Name); dstCloudBlobContainer.CreateIfNotExists(); //Assuming the source blob container ACL is "Private", let's create a Shared Access Signature with //Start Time = Current Time (UTC) - 15 minutes to account for Clock Skew //Expiry Time = Current Time (UTC) + 7 Days - 7 days is the maximum time allowed for copy operation to finish. //Permission = Read so that copy service can read the blob from source var sas = srcCloudBlobContainer.GetSharedAccessSignature(new SharedAccessBlobPolicy() { SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-15), SharedAccessExpiryTime = DateTime.UtcNow.AddDays(7), Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.List | SharedAccessBlobPermissions.Read | SharedAccessBlobPermissions.Delete, }); string blobPrefix = null; bool useFlatBlobListing = true; foreach (var srcBlob in srcCloudBlobContainer.ListBlobs(blobPrefix, useFlatBlobListing, BlobListingDetails.None)) { if (srcBlob.GetType() == typeof(CloudBlockBlob)) { var srcBlockBlock = (CloudBlockBlob)srcBlob; var dstBlockBlock = dstCloudBlobContainer .GetBlockBlobReference(srcBlockBlock.Name); srcBlockBlock.FetchAttributes(); if (!dstBlockBlock.Exists()) { //Create a SAS URI for the blob var srcBlockBlobSasUri = string.Format("{0}{1}", srcBlockBlock.Uri, sas); // throws exception StorageException: // The remote server returned an error: (404) Not Found. dstBlockBlock.StartCopyFromBlob(new Uri(srcBlockBlobSasUri)); dstBlockBlock.CreateSnapshot(); } else { dstBlockBlock.FetchAttributes(); if (dstBlockBlock.Metadata.Keys.Count == 3) { if ((dstBlockBlock.Metadata["hashValue"] != srcBlockBlock.Metadata["hashValue"]) || (dstBlockBlock.Metadata["timestamp"] != srcBlockBlock.Metadata["timestamp"])) { //Create a SAS URI for the blob var srcBlockBlobSasUri = string.Format("{0}{1}", srcBlockBlock.Uri, sas); // throws exception StorageException: // The remote server returned an error: (404) Not Found. dstBlockBlock.StartCopyFromBlob(new Uri(srcBlockBlobSasUri)); dstBlockBlock.Metadata["hashValue"] = srcBlockBlock.Metadata["hashValue"]; dstBlockBlock.Metadata["timestamp"] = srcBlockBlock.Metadata["timestamp"]; dstBlockBlock.Metadata["filePath"] = srcBlockBlock.Metadata["filePath"]; dstBlockBlock.SetMetadata(); dstBlockBlock.CreateSnapshot(); } } else { //Create a SAS URI for the blob var srcBlockBlobSasUri = string.Format("{0}{1}", srcBlockBlock.Uri, sas); // throws exception StorageException: // The remote server returned an error: (404) Not Found. dstBlockBlock.StartCopyFromBlob(new Uri(srcBlockBlobSasUri)); dstBlockBlock.Metadata["hashValue"] = srcBlockBlock.Metadata["hashValue"]; dstBlockBlock.Metadata["timestamp"] = srcBlockBlock.Metadata["timestamp"]; dstBlockBlock.Metadata["filePath"] = srcBlockBlock.Metadata["filePath"]; dstBlockBlock.SetMetadata(); dstBlockBlock.CreateSnapshot(); } } } } } } }
private void delete(CloudBlobClient srcBlobClient, CloudBlobClient dstBlobClient) { foreach (var dstCloudBlobContainer in dstBlobClient.ListContainers()) { if (dstCloudBlobContainer.Name != "vhds") { var srcCloudBlobContainer = srcBlobClient.GetContainerReference(dstCloudBlobContainer.Name); if (!srcCloudBlobContainer.Exists()) { dstCloudBlobContainer.Delete(); } else { foreach (var dstBlob in dstCloudBlobContainer.ListBlobs()) { if (dstBlob.GetType() == typeof(CloudBlockBlob)) { var dstBlockBlob = (CloudBlockBlob)dstBlob; var srcBlockBlob = srcCloudBlobContainer.GetBlockBlobReference(dstBlockBlob.Name); if (!srcBlockBlob.Exists()) { dstBlockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots); } } } } } } }