Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created February 7, 2018 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmatskas/3d94f96316c06fba8f1afb3d4cbf8635 to your computer and use it in GitHub Desktop.
Save cmatskas/3d94f96316c06fba8f1afb3d4cbf8635 to your computer and use it in GitHub Desktop.
Function to retrieve deleted blobs and put them in a queue
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs.Host;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage;
using System.Linq;
using System;
namespace SearchFunctions
{
public static class Function1
{
[FunctionName("RetrieveDeletedBlobs")]
public static async Task<ICollector<string>> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req,
[Queue("blobs-to-delete", Connection = "StorageQueueConnectionString")] ICollector<string> outputQueueItems,
TraceWriter log)
{
log.Info("Adding blobs to the delete queue");
var deletedBlobUris = await ListAllDeletedBlobUris();
foreach (var blobUri in deletedBlobUris)
{
outputQueueItems.Add(blobUri);
}
return outputQueueItems;
}
private static async Task<List<string>> ListAllDeletedBlobUris()
{
var sa = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("StorageBlobConnectionString"));
var blobClient = sa.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("searchdata");
var results = await ListBlobsAsync(container);
var deletedBlobs = results.Cast<CloudBlockBlob>()
.Where(b => b.Metadata["IsDeleted"] == "false");
return deletedBlobs.Select(b => b.Uri.AbsoluteUri).ToList();
}
public static async Task<List<IListBlobItem>> ListBlobsAsync(CloudBlobContainer container)
{
BlobContinuationToken continuationToken = null;
List<IListBlobItem> results = new List<IListBlobItem>();
do
{
var response = await container.ListBlobsSegmentedAsync(
null,
false,
BlobListingDetails.Metadata,
null,
continuationToken,
null, null);
continuationToken = response.ContinuationToken;
results.AddRange(response.Results);
}
while (continuationToken != null);
return results;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment