# Writing to AWS S3 from Fly.io (in Go)

DevFeed: [Writing to AWS S3 from Fly.io (in Go)](<https://devfeed.tech/articles/writing-to-aws-s3-from-fly-io-in-go-89758.md>)

Original publisher: [Read original article](<https://www.jvt.me/posts/2026/10/18/fly-s3/>)

Author: Jamie Tanna

Published: 2026-10-18T16:46:39Z

Content type: tutorial

Language: en

Sources: [Jamie Tanna](<https://devfeed.tech/sources/articles-on-jamie-tanna-software-engineer.md>)

Topics: [AWS Security Token Service](<https://devfeed.tech/topics/aws-security-token-service.md>), [fly.io](<https://devfeed.tech/topics/fly-io.md>), [OpenID connect (OIDC)](<https://devfeed.tech/topics/oidc.md>), [IO](<https://devfeed.tech/topics/io.md>)

Tags: [aws](<https://devfeed.tech/tags/aws.md>), [blogumentation](<https://devfeed.tech/tags/blogumentation.md>), [fly](<https://devfeed.tech/tags/fly.md>), [fly-io](<https://devfeed.tech/tags/fly-io.md>), [go](<https://devfeed.tech/tags/go.md>), [oidc](<https://devfeed.tech/tags/oidc.md>), [s3](<https://devfeed.tech/tags/s3.md>)

## AI overview

A Go tutorial shows how to obtain a short-lived OpenID Connect token from Fly.io, exchange it through AWS STS for temporary credentials, and use those credentials to write objects to S3. It also gives example IAM trust and bucket policies and notes that overly restrictive trust-policy conditions caused an access-denied error.

## Source excerpt

I'm currently in the process of moving my site's media (under media.jvt.me) from Netlify to AWS S3. The underlying infrastructure for my site that writes this media is hosted on Fly.io, so needs a way to write to S3. I really wanted to avoid long-lived AWS credentials, and fortunately Fly supports OpenID Connect which integrates with AWS' OpenID Connect Federation to allow you to use short-lived OpenID Connect tokens to authenticate as a given IAM role, and then perform any necessary AWS actions. Although Fly has an example with curl, I wanted to blogument the "how to" with Go, as it wasn't super straightforward to do so. I've written this up in a sample repo on GitLab.com. Go code The core pieces are: // via https://gist.github.com/teknoraver/5ffacb8757330715bcbcc90e6d46ac74 var unixClient = http.Client{ Transport: &http.Transport{ DialContext: func(_ context.Context, _, _ string) (net.Conn, error) { return net.Dial("unix", "/.fly/api") }, }, } func getOIDCToken(unixClient http.Client) (string, error) { s3Body := bytes.NewReader([]byte(`{"aud":"sts.amazonaws.com"}`)) // or could be `{"aud":"https://sts.amazonaws.com"}` as long as it matches your Trust Policy // get the OIDC token resp, err := unixClient.Post("http://localhost/v1/tokens/oidc", "application/json", s3Body) if err != nil { slog.Error("Failed to call Fly OIDC tokens endpoint", "err", err) return "", err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { slog.Error("Failed to read body", "err", err) return "", err } return string(body), nil } func newAWSClient(oidcToken string) (*s3.Client, error) { cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("eu-west-1")) if err != nil { return nil, err } stsClient := sts.NewFromConfig(cfg) assumedRoleInput := &sts.AssumeRoleWithWebIdentityInput{ RoleArn: aws.String(os.Getenv("AWS_ASSUME_ROLE_ARN")), RoleSessionName: aws.String("..."), WebIdentityToken: aws.String(oidcToken), } resp, err := stsClient.AssumeRoleWithWeb