presign_post

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

README

Custom PresignClient for presigned POST using the AWS s3 Go SDK (v2)

Why?

The official SDK has not had a way to create presigned POST urls, despite having an open issue and associated PR for over 2 years. This module adds support for creating these presigned POST urls in the cleanest way possible

Usage

package main

import (
	"context"
	"fmt"
	"time"

	presign_post "github.com/PretendoNetwork/aws-sdk-presigned-post-go"
	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/credentials"
)

func main() {
	s3Endpoint := "https://s3.example.com"
	s3Region := "us-east-1"
	s3AccessKey := "key_id"
	s3AccessSecret := "key_secret"
	bucket := "bucket"
	key := "file.bin"

	staticCredentials := credentials.NewStaticCredentialsProvider(s3AccessKey, s3AccessSecret, "")

	endpointResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
		return aws.Endpoint{
			URL: s3Endpoint,
		}, nil
	})

	cfg, err := config.LoadDefaultConfig(
		context.TODO(),
		config.WithRegion(s3Region),
		config.WithCredentialsProvider(staticCredentials),
		config.WithEndpointResolverWithOptions(endpointResolver),
	)

	if err != nil {
		panic(err)
	}

	presignPostClient := presign_post.NewPresignClient(cfg)

	input := &presign_post.PostObjectInput{
		Bucket:    bucket,
		Key:       key,
		ExpiresIn: time.Minute * 15,
	}

	res, _ := presignPostClient.PresignPostObject(input)

	fmt.Printf("%+v\n", res)
}

Disclaimers

  1. This has only been tested on DigitalOcean Spaces, though it should work on any s3-compatible server
  2. I was not able to find a way to get pre-formatted endpoints from the config alone, so a small regex is used to find and replace inside the endpoint string to add the bucket

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PostObjectInput

type PostObjectInput struct {
	// Key name
	Key string

	// The name of the bucket to presign the post to
	Bucket string

	// Expiration -  The number of seconds the presigned post is valid for.
	ExpiresIn time.Duration

	// A list of conditions to include in the policy. Each element can be either a list or a structure.
	// For example:
	// [
	//      {"acl": "public-read"}, ["content-length-range", 2, 5], ["starts-with", "$success_action_redirect", ""]
	// ]
	Conditions []interface{}
}

type PresignClient

type PresignClient struct {
	// contains filtered or unexported fields
}

func NewPresignClient

func NewPresignClient(cfg aws.Config) *PresignClient

func (*PresignClient) PresignPostObject

func (presignClient *PresignClient) PresignPostObject(input *PostObjectInput) (*PresignedPostObject, error)

type PresignedPostObject

type PresignedPostObject struct {
	URL           string `json:"url"`
	Key           string `json:"key"`
	Policy        string `json:"policy"`
	Credential    string `json:"credential"`
	SecurityToken string `json:"securityToken,omitempty"`
	Signature     string `json:"signature"`
	Date          string `json:"date"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL