How to delete files from blob container?

private readonly CloudBlobContainer _blobContainer; public void Remove() { if (_blobContainer.Exists()) { _blobContainer.Delete(); } } 

How to remove not the entire container, but some List<string> disks that is in the container?

+12
source share
3 answers

This is the code I'm using:

 private CloudBlobContainer blobContainer; public void DeleteFile(string uniqueFileIdentifier) { this.AssertBlobContainer(); var blob = this.blobContainer.GetBlockBlobReference(fileName); blob.DeleteIfExists(); } private void AssertBlobContainer() { // only do once if (this.blobContainer == null) { lock (this.blobContainerLockObj) { if (this.blobContainer == null) { var client = this.cloudStorageAccount.CreateCloudBlobClient(); this.blobContainer = client.GetContainerReference(this.containerName.ToLowerInvariant()); if (!this.blobContainer.Exists()) { throw new CustomRuntimeException("Container {0} does not exist in azure account", containerName); } } } } if (this.blobContainer == null) throw new NullReferenceException("Blob Empty"); } 

You can ignore the lock code if you know that it will not be available at the same time.

Obviously, you sorted the contents of blobContainer , so all you need is the DeleteFile method without this.AssertBlobContainer() .

+23
source
 List<string> FileNameList = new List<string>(); FileNameList = fileName.Split(',').Where(t => t.ToString().Trim() != "").ToList(); CloudBlobClient client; CloudBlobContainer container; CloudBlockBlob blob; string accessKey; string accountName; string connectionString; accessKey = Environment.GetEnvironmentVariable("StorageAccountaccessKey"); accountName = Environment.GetEnvironmentVariable("StorageAccountName"); connectionString = Environment.GetEnvironmentVariable("StorageAccountConnectionString"); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); client = storageAccount.CreateCloudBlobClient(); string containerName = tenantId; container = client.GetContainerReference(containerName); foreach(var file in FileNameList) { blob = container.GetBlockBlobReference(file); blob.DeleteIfExists(); } 
+1
source

We can use cloudBlobContainer.ListBlobsSegmentedAsync to make cloudBlobContainer.ListBlobsSegmentedAsync list of cloudBlobContainer.ListBlobsSegmentedAsync objects and then cast them to ICloudBlob so that you can execute DeleteIfExistsAsync . The following is the function of the working sample. Hope it helps.

 public async Task < bool > PerformTasks() { try { if (CloudStorageAccount.TryParse(StorageConnectionString, out CloudStorageAccount cloudStorageAccount)) { var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); var cloudBlobContainer = cloudBlobClient.GetContainerReference(_blobContainerName); if (await cloudBlobContainer.ExistsAsync()) { BlobContinuationToken blobContinuationToken = null; var blobList = await cloudBlobContainer.ListBlobsSegmentedAsync(blobContinuationToken); foreach(var item in blobList.Results.Select(blb = >blb as ICloudBlob)) { await item.DeleteIfExistsAsync(); } } else { _logger.LogError(ErrorMessages.NoStorageConnectionStringAvailable); } } else { _logger.LogError(ErrorMessages.NoStorageConnectionStringAvailable); } } catch(Exception ex) { _logger.LogError(ex.Message); } return false; } 
0
source

All Articles