本文整理汇总了C#中Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.DeleteIfExistsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# CloudBlobContainer.DeleteIfExistsAsync方法的具体用法?C# CloudBlobContainer.DeleteIfExistsAsync怎么用?C# CloudBlobContainer.DeleteIfExistsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
的用法示例。
在下文中一共展示了CloudBlobContainer.DeleteIfExistsAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BasicStorageBlockBlobOperationsWithAccountSASAsync
/// <summary>
/// Basic operations to work with block blobs
/// </summary>
/// <returns>Task<returns>
private static async Task BasicStorageBlockBlobOperationsWithAccountSASAsync()
{
const string imageToUpload = "HelloWorld.png";
string blockBlobContainerName = "demoblockblobcontainer-" + Guid.NewGuid();
// Call GetAccountSASToken to get a sasToken based on the Storage Account Key
string sasToken = GetAccountSASToken();
// Create an AccountSAS from the SASToken
StorageCredentials accountSAS = new StorageCredentials(sasToken);
//Informational: Print the Account SAS Signature and Token
Console.WriteLine();
Console.WriteLine("Account SAS Signature: " + accountSAS.SASSignature);
Console.WriteLine("Account SAS Token: " + accountSAS.SASToken);
Console.WriteLine();
// Create a container for organizing blobs within the storage account.
Console.WriteLine("1. Creating Container using Account SAS");
// Get the Container Uri by passing the Storage Account and the container Name
Uri ContainerUri = GetContainerSASUri(blockBlobContainerName);
// Create a CloudBlobContainer by using the Uri and the sasToken
CloudBlobContainer container = new CloudBlobContainer(ContainerUri, new StorageCredentials(sasToken));
try
{
await container.CreateIfNotExistsAsync();
}
catch (StorageException)
{
Console.WriteLine("If you are running with the default configuration please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
Console.ReadLine();
throw;
}
// To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
// to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
// await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
// Upload a BlockBlob to the newly created container
Console.WriteLine("2. Uploading BlockBlob");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(imageToUpload);
await blockBlob.UploadFromFileAsync(imageToUpload, FileMode.Open);
// List all the blobs in the container
Console.WriteLine("3. List Blobs in Container");
foreach (IListBlobItem blob in container.ListBlobs())
{
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
// Use blob.GetType() and cast to appropriate type to gain access to properties specific to each type
Console.WriteLine("- {0} (type: {1})", blob.Uri, blob.GetType());
}
// Download a blob to your file system
Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", imageToUpload), FileMode.Create);
// Create a read-only snapshot of the blob
Console.WriteLine("5. Create a read-only snapshot of the blob");
CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null);
// Clean up after the demo
Console.WriteLine("6. Delete block Blob and all of its snapshots");
await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
Console.WriteLine("7. Delete Container");
await container.DeleteIfExistsAsync();
}
示例2: DeleteContainerAsync
public async Task<bool> DeleteContainerAsync(string containerName)
{
// Delete the container if it doesn't exist.
blobContainer = blobClient.GetContainerReference(containerName);
return await blobContainer.DeleteIfExistsAsync();
}
示例3: BasicStorageBlockBlobOperationsWithAccountSASAsync
/// <summary>
/// Basic operations to work with block blobs
/// </summary>
/// <returns>A Task object.</returns>
private static async Task BasicStorageBlockBlobOperationsWithAccountSASAsync()
{
const string ImageToUpload = "HelloWorld.png";
string containerName = ContainerPrefix + Guid.NewGuid();
// Get an account SAS token.
string sasToken = GetAccountSASToken();
// Use the account SAS token to create authentication credentials.
StorageCredentials accountSAS = new StorageCredentials(sasToken);
// Informational: Print the Account SAS Signature and Token.
Console.WriteLine();
Console.WriteLine("Account SAS Signature: " + accountSAS.SASSignature);
Console.WriteLine("Account SAS Token: " + accountSAS.SASToken);
Console.WriteLine();
// Get the URI for the container.
Uri containerUri = GetContainerUri(containerName);
// Get a reference to a container using the URI and the SAS token.
CloudBlobContainer container = new CloudBlobContainer(containerUri, accountSAS);
try
{
// Create a container for organizing blobs within the storage account.
Console.WriteLine("1. Creating Container using Account SAS");
await container.CreateIfNotExistsAsync();
}
catch (StorageException e)
{
Console.WriteLine(e.ToString());
Console.WriteLine("If you are running with the default configuration, please make sure you have started the storage emulator. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
Console.ReadLine();
throw;
}
try
{
// To view the uploaded blob in a browser, you have two options. The first option is to use a Shared Access Signature (SAS) token to delegate
// access to the resource. See the documentation links at the top for more information on SAS. The second approach is to set permissions
// to allow public access to blobs in this container. Uncomment the line below to use this approach. Then you can view the image
// using: https://[InsertYourStorageAccountNameHere].blob.core.windows.net/democontainer/HelloWorld.png
// await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
// Upload a BlockBlob to the newly created container
Console.WriteLine("2. Uploading BlockBlob");
CloudBlockBlob blockBlob = container.GetBlockBlobReference(ImageToUpload);
await blockBlob.UploadFromFileAsync(ImageToUpload);
// List all the blobs in the container
Console.WriteLine("3. List Blobs in Container");
BlobContinuationToken token = null;
do
{
BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);
token = resultSegment.ContinuationToken;
foreach (IListBlobItem blob in resultSegment.Results)
{
// Blob type will be CloudBlockBlob, CloudPageBlob or CloudBlobDirectory
Console.WriteLine("{0} (type: {1}", blob.Uri, blob.GetType());
}
}
while (token != null);
// Download a blob to your file system
Console.WriteLine("4. Download Blob from {0}", blockBlob.Uri.AbsoluteUri);
await blockBlob.DownloadToFileAsync(string.Format("./CopyOf{0}", ImageToUpload), FileMode.Create);
// Create a read-only snapshot of the blob
Console.WriteLine("5. Create a read-only snapshot of the blob");
CloudBlockBlob blockBlobSnapshot = await blockBlob.CreateSnapshotAsync(null, null, null, null);
// Delete the blob and its snapshots.
Console.WriteLine("6. Delete block Blob and all of its snapshots");
await blockBlob.DeleteIfExistsAsync(DeleteSnapshotsOption.IncludeSnapshots, null, null, null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
finally
{
// Clean up after the demo.
// Note that it is not necessary to delete all of the blobs in the container first; they will be deleted
// with the container.
Console.WriteLine("7. Delete Container");
await container.DeleteIfExistsAsync();
}
}