Skip to content

Instantly share code, notes, and snippets.

@lenadroid lenadroid/fsharp-azure-function.fsx Secret
Last active Sep 28, 2018

Embed
What would you like to do?
#r "System.Net.Http"
#r "Newtonsoft.Json"
#r "FSharp.Data"
open System
open FSharp.Data
open FSharp.Data.JsonExtensions
open System.Net
open System.Net.Http
open Newtonsoft.Json
let Run(req: HttpRequestMessage, log: TraceWriter) =
async {
log.Info(sprintf
"Hello from F# function on Azure!")
let storageAccountName = "<name-of-your-storage-account>"
let pathToFileInStorage = "test/text.txt"
let resourceUrl = "https://storage.azure.com/"
// Set name to query string
let name =
req.GetQueryNameValuePairs()
|> Seq.tryFind (fun q -> q.Key = "name")
let accessTokenRequest =
Http.RequestString(
Environment.GetEnvironmentVariable("MSI_ENDPOINT"),
headers = [ "secret", Environment.GetEnvironmentVariable("MSI_SECRET") ],
httpMethod="GET",
query = ["api-version", "2017-09-01"; "resource", resourceUrl ])
|> JsonValue.Parse
let accessToken = accessTokenRequest?access_token.AsString()
log.Info(sprintf "Access Token: " + accessToken)
let fileContent =
Http.RequestString(
("https://" + storageAccountName + ".blob.core.windows.net/" + pathToFileInStorage),
headers = [ "Authorization", ("Bearer " + accessToken); "x-ms-version", "2017-11-09" ],
httpMethod="GET")
log.Info(sprintf "File Content: " + fileContent)
match name with
| Some x ->
return req.CreateResponse(HttpStatusCode.OK, "Hello " + x.Value + "! File Content: '" + fileContent + "'")
| None ->
return req.CreateResponse(HttpStatusCode.OK, "File content: '" + fileContent + "'")
} |> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.