本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient.ListContainers方法的典型用法代码示例。如果您正苦于以下问题:C# CloudBlobClient.ListContainers方法的具体用法?C# CloudBlobClient.ListContainers怎么用?C# CloudBlobClient.ListContainers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.Blob.CloudBlobClient
的用法示例。
在下文中一共展示了CloudBlobClient.ListContainers方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
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();
}
}
}
}
示例2: DeleteContainersWithPrefixAsync
/// <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;
}
}
示例3: ManageContainerLeasesAsync
//.........这里部分代码省略.........
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();
}
}
}
示例4: ListAllContainers
/// <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;
}
}
示例5: ConnectToStorage
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;
}
示例6: LoadLeftPane
//.........这里部分代码省略.........
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);
}
}
示例7: CleanupImportClient
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();
}
}
示例8: InitializeImportClient
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);
}
}
示例9: ListContainersAsync
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);
}
示例10: Load
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);
});
}
示例11: Copy
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();
}
}
}
}
}
}
}
示例12: delete
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);
}
}
}
}
}
}
}