Created
September 26, 2022 08:19
-
-
Save xocasdashdash/ec390cb55a114dac0c86654e03c8589c to your computer and use it in GitHub Desktop.
Generate a presigned URL for AWS IAM
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
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