Skip to content

Instantly share code, notes, and snippets.

@slav123
Created June 23, 2022 14:16
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 slav123/910f2485d1ed74bf684917e67699d9b3 to your computer and use it in GitHub Desktop.
Save slav123/910f2485d1ed74bf684917e67699d9b3 to your computer and use it in GitHub Desktop.
PUT presigned url example in Go
// main.go
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"log"
"os"
)
func main() {
ctx := context.Background()
var (
cfg aws.Config
err error
)
if os.Getenv("EndPoint") == "" {
// aws default config
cfg, err = config.LoadDefaultConfig(ctx, // Hard coded credentials.
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: os.Getenv("AccessKeyID"), SecretAccessKey: os.Getenv("SecretAccessKey"), SessionToken: "",
Source: "example hard coded credentials",
},
}),
)
} else {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
//URL: fmt.Sprintf("https://%s.r2.cloudflarestorage.com", accountId),
URL: os.Getenv("EndPoint"),
}, nil
})
// aws default config
cfg, err = config.LoadDefaultConfig(ctx, // Hard coded credentials.
config.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: os.Getenv("AccessKeyID"), SecretAccessKey: os.Getenv("SecretAccessKey"), SessionToken: "",
Source: "example hard coded credentials",
},
}),
config.WithEndpointResolverWithOptions(customResolver))
}
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
key := "main.go"
client := s3.NewFromConfig(cfg)
input := &s3.PutObjectInput{
Bucket: aws.String(os.Getenv("Bucket")),
Key: aws.String(key),
}
psClient := s3.NewPresignClient(client)
resp, err := PutPresignedURL(ctx, psClient, input)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("curl \"%s\" --upload-file main.go\n", resp.URL)
}
// S3PresignGetObjectAPI defines the interface for the PresignGetObject function.
// We use this interface to test the function using a mocked service.
type S3PresignGetObjectAPI interface {
PresignGetObject(
ctx context.Context,
params *s3.GetObjectInput,
optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
}
// S3PresignedPutObjectAPI defines the interface for the PresignPutObject function.
// We use this interface to test the function using a mocked service.
type S3PresignedPutObjectAPI interface {
PresignPutObject(
ctx context.Context,
params *s3.PutObjectInput,
optFns ...func(*s3.PresignOptions)) (*v4.PresignedHTTPRequest, error)
}
// GetPresignedURL retrieves a presigned URL for uploading S3 bucket object.
// Inputs:
// c is the context of the method call, which includes the AWS Region.
// api is the interface that defines the method call.
// input defines the input arguments to the service call.
// Output:
// If successful, the presigned URL for the object and nil.
// Otherwise, nil and an error from the call to PresignGetObject.
func GetPresignedURL(c context.Context, api S3PresignGetObjectAPI, input *s3.GetObjectInput) (*v4.PresignedHTTPRequest, error) {
return api.PresignGetObject(c, input)
}
// PutPresignedURL retrieves a presigned URL for uploading S3 bucket object.
func PutPresignedURL(c context.Context, api S3PresignedPutObjectAPI, input *s3.PutObjectInput) (*v4.PresignedHTTPRequest, error) {
return api.PresignPutObject(c, input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment