A small, self-contained AWS client for S3 and Secrets Manager, built on
aws-sdk-go-v2. It has no dependency on
any service's config package, so it can be shared across repos.
go get github.com/nuzur/awsConfig holds the credentials and target settings. It is provided explicitly —
there is no implicit config-file fallback.
cfg := &aws.Config{
Region: "us-east-1",
KeyID: "AKIA...",
Secret: "...",
Bucket: "my-bucket",
KmsKeyID: "arn:aws:kms:...", // optional; used for server-side encryption
}client, err := aws.New(aws.Params{Config: cfg})
if err != nil {
log.Fatal(err)
}
// Upload (server-side encrypted with the configured KMS key when encrypt=true).
out, err := client.UploadToS3(ctx, "path/file.png", data, nil, true)
// Presigned GET URL.
url, err := client.GetSignedS3URL(ctx, "path/file.png", 15*time.Minute)
// Download.
bytes, err := client.FetchFromS3(ctx, "path/file.png")
// Secrets Manager.
secret, err := client.FetchSecret(ctx, "my/secret")
_, err = client.CreateSecret(ctx, "my/secret", `{}`, &cfg.KmsKeyID)
_, err = client.UpdateSecret(ctx, "my/secret", `{"k":"v"}`, nil)All request methods take a context.Context. New performs a one-time config
load and is safe to wire as an fx provider:
fx.Provide(
func() *aws.Config { return cfg },
aws.New,
)Use AWSConfig() to construct additional aws-sdk-go-v2 service clients from
the same credentials (for example, SES):
ses := sesv2.NewFromConfig(client.AWSConfig())UploadHandler returns an http.HandlerFunc that accepts a multipart file
form field, uploads it under keyPrefix, and returns the resulting file path as
JSON:
mux.Handle("/upload", aws.UploadHandler(client, "nem/"))