Skip to content

Instantly share code, notes, and snippets.

@xocasdashdash
Created September 26, 2022 08:19
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 xocasdashdash/ec390cb55a114dac0c86654e03c8589c to your computer and use it in GitHub Desktop.
Save xocasdashdash/ec390cb55a114dac0c86654e03c8589c to your computer and use it in GitHub Desktop.
Generate a presigned URL for AWS IAM
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic("configuration error, " + err.Error())
}
svc := sts.NewFromConfig(cfg)
presignClient := sts.NewPresignClient(svc)
sts.WithPresignClientFromClientOptions()
appendPresignHeaderValuesFunc := func(server string) func(stsOptions *sts.Options) {
return func(stsOptions *sts.Options) {
// Add clientName Header
stsOptions.APIOptions = append(stsOptions.APIOptions, smithyhttp.SetHeaderValue("X-server-name", server))
// Add X-Amz-Expires query param
stsOptions.APIOptions = append(stsOptions.APIOptions, smithyhttp.SetHeaderValue("X-Amz-Expires", "60"))
}
}
getCallerIdentity, err := presignClient.PresignGetCallerIdentity(context.Background(), &sts.GetCallerIdentityInput{}, func(presignOptions *sts.PresignOptions) {
presignOptions.ClientOptions = append(presignOptions.ClientOptions, appendPresignHeaderValuesFunc("api-gw-service"))
})
if err != nil {
panic(err)
}
fmt.Printf("%s", getCallerIdentity.URL)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment