Created
February 14, 2018 01:34
-
-
Save romeshniriella/35ff6d3896c8f91ed751d74ff5c3ab8f to your computer and use it in GitHub Desktop.
Azure function to get all the blob storage containers in my storage account
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.Azure.WebJobs.Host; | |
namespace AzBlobFuncApp | |
{ | |
public static class FunctionGetBlobContainers | |
{ | |
/// <summary> | |
/// AZ function triggered by HTTP GET to return all the blob storage containers in our system. | |
/// This is going to be used by logic apps. | |
/// </summary> | |
/// <param name="req"></param> | |
/// <param name="log"></param> | |
/// <returns></returns> | |
[FunctionName("GetBlobContainers")] | |
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage req, TraceWriter log) | |
{ | |
log.Info($"{DateTime.UtcNow} : Executing azFunc GetBlobContainers()"); | |
var azConnectionString = GetEnvironmentVariable("StorageConnectionString"); | |
var storageHelper = new StorageHelper( | |
log, | |
azConnectionString); | |
// we only need names of the containers | |
var containers = storageHelper.GetContainers().Select(x=>x.Name).ToList(); | |
return req.CreateResponse(HttpStatusCode.OK, containers); | |
} | |
/// <summary> | |
/// retrieves the env variable from local.settings.json or from settings once deployed. | |
/// </summary> | |
/// <remarks> | |
/// env var is stored in Values:{"something"}</remarks> | |
/// <param name="name"></param> | |
/// <returns></returns> | |
public static string GetEnvironmentVariable(string name) | |
{ | |
return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
using Microsoft.WindowsAzure.Storage.RetryPolicies; | |
namespace AzBlobFuncApp | |
{ | |
/// <summary> | |
/// Helper class to connect to az blob storage and get containers | |
/// </summary> | |
public class StorageHelper | |
{ | |
private const int RetryDeltaBackOff = 4; | |
private const int RetryMaxAttemts = 5; | |
private const int RetryMaxExecutionTime = 30; | |
private readonly TraceWriter _logger; | |
private readonly string _connectionString; | |
public StorageHelper( | |
TraceWriter logger, | |
string sourceConnectionString) | |
{ | |
_logger = logger; | |
_connectionString = sourceConnectionString; | |
} | |
/// <summary> | |
/// Returns all the containers in our storage account. | |
/// Retries if any error occurs for resiliency. | |
/// </summary> | |
/// <returns></returns> | |
public IEnumerable<CloudBlobContainer> GetContainers() | |
{ | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionString); | |
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); | |
blobClient.DefaultRequestOptions.RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(RetryDeltaBackOff), RetryMaxAttemts); | |
blobClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(RetryMaxExecutionTime); | |
return blobClient.ListContainers(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment