Generate Signed S3 URL in F#

If you have a single page application, you may be wanting to upload files directly to S3 from the browser rather than using your own server side application as a middle man between the two.

In F# this can be done with the Amazon S3 package

Via dotnet cli: dotnet add package AWSSDK.S3 --version CURRENT_VERSION

Via paket: paket add AWSSDK.S3 --version CURRENT_VERSION

Once the library is installed, we can make a function that handles generating the presigned URL. This function needs access to your AWS public and private keys for credentials, your bucket and region, as well as the key of your object being Ā uploaded. All of the variables should be environment variables, except the object key, which the function takes as an argument.

let generateS3PresignedUrl key =
   let credentials =
       BasicAWSCredentials(awsAccessKey, awsSecretKey)

    let client =
        new AmazonS3Client(credentials, awsRegion)

    let request = GetPreSignedUrlRequest()
    request.BucketName <- bucketName
    request.Key <- key |> string
    request.ContentType <- "content/type/here"
    request.Expires <- DateTime.Now.AddMinutes 1
    request.Parameters[ "cache-control" ] <-
        System.Web.HttpUtility.UrlEncode "max-age=MAX_AGE_HERE;"
    request.Parameters[ "amz-acl" ] <- "ACL HERE"
    request.Verb <- HttpVerb.PUT

    client.GetPreSignedURL(request)