-
-
Save lenadroid/f6282bbdd05862962cec842792f9166d to your computer and use it in GitHub Desktop.
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
#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