simples3

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2018 License: BSD-2-Clause Imports: 19 Imported by: 9

README

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing

Overview GoDoc Go Report Card

SimpleS3 is a golang library for uploading and deleting objects on S3 buckets using REST API calls or Presigned URLs signed using AWS Signature Version 4.

Install

go get github.com/rhnvrm/simples3

Example

testTxt, _ := os.Open("test.txt")
defer testTxt.Close()

// Create an instance of the package
// You can either create by manually supplying credentials
// (preferably using Environment vars)
s3 := simples3.New(Region, AWSAccessKey, AWSSecretKey)
// or you can use this on an EC2 instance to 
// obtain credentials from IAM attached to the instance.
s3 := simples3.NewUsingIAM(Region)

// File Upload is as simple as providing the following
// details.
err := s3.FileUpload(simples3.UploadInput{
    Bucket:      AWSBucket,
    ObjectKey:   "test.txt",
    ContentType: "text/plain",
    FileName:    "test.txt",
    Body:        testTxt,
}

// Similarly, Files can be deleted.
err := s3.FileDelete(simples3.DeleteInput{
    Bucket:    os.Getenv("AWS_S3_BUCKET"),
    ObjectKey: "test.txt",
}

// You can also use this library to generate
// Presigned URLs that can for eg. be used to
// GET/PUT files on S3 through the browser.
var time, _ = time.Parse(time.RFC1123, "Fri, 24 May 2013 00:00:00 GMT")

url := s.GeneratePresignedURL(PresignedInput{
    Bucket:        "examplebucket",
    ObjectKey:     "test.txt",
    Method:        "GET",
    Timestamp:     time,
    ExpirySeconds: 86400,
})

Contributing

You are more than welcome to contribute to this project. Fork and make a Pull Request, or create an Issue if you see any problem or want to propose a feature.

Contributors

Author

Rohan Verma hello@rohanverma.net

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NowTime = func() time.Time {
	return time.Now()
}

NowTime mockable time.Now()

Functions

This section is empty.

Types

type DeleteInput

type DeleteInput struct {
	Bucket    string
	ObjectKey string
}

DeleteInput is passed to FileDelete as a parameter.

type IAMResponse

type IAMResponse struct {
	Code            string `json:"Code"`
	LastUpdated     string `json:"LastUpdated"`
	Type            string `json:"Type"`
	AccessKeyID     string `json:"AccessKeyId"`
	SecretAccessKey string `json:"SecretAccessKey"`
	Token           string `json:"Token"`
	Expiration      string `json:"Expiration"`
}

IAMResponse is used by NewUsingIAM to auto detect the credentials

type PolicyJSON

type PolicyJSON struct {
	Expiration string        `json:"expiration"`
	Conditions []interface{} `json:"conditions"`
}

PolicyJSON is policy rule

type PresignedInput

type PresignedInput struct {
	Bucket        string
	ObjectKey     string
	Method        string
	Timestamp     time.Time
	ExtraHeaders  map[string]string
	ExpirySeconds int
}

type S3

type S3 struct {
	AccessKey string
	SecretKey string
	Region    string

	URIFormat string
}

S3 provides a wrapper around your S3 credentials.

func New

func New(region, accessKey, secretKey string) *S3

New returns an instance of S3.

func NewUsingIAM

func NewUsingIAM(region string) (*S3, error)

NewUsingIAM automatically generates an Instance of S3 using instance metatdata.

func (*S3) CreateUploadPolicies

func (s3 *S3) CreateUploadPolicies(uploadConfig UploadConfig) (UploadPolicies, error)

CreateUploadPolicies creates amazon s3 sigv4 compatible policy and signing keys with the signature returns the upload policy. https://docs.aws.amazon.com/ja_jp/AmazonS3/latest/API/sigv4-authentication-HTTPPOST.html

func (*S3) FileDelete

func (s3 *S3) FileDelete(u DeleteInput) error

FileDelete makes a DELETE call with the file written as multipart and on successful upload, checks for 204 No Content.

func (*S3) FileUpload

func (s3 *S3) FileUpload(u UploadInput) error

FileUpload makes a POST call with the file written as multipart and on successful upload, checks for 200 OK.

func (*S3) GeneratePresignedURL

func (s3 *S3) GeneratePresignedURL(in PresignedInput) string

type UploadConfig

type UploadConfig struct {
	// Required
	BucketName  string
	ObjectKey   string
	ContentType string
	FileSize    int64
	// Optional
	UploadURL  string
	Expiration time.Duration
	MetaData   map[string]string
}

UploadConfig generate policies from config for POST requests to S3 using Signing V4.

type UploadInput

type UploadInput struct {
	Bucket      string
	ObjectKey   string
	FileName    string
	ContentType string
	Body        multipart.File
}

UploadInput is passed to FileUpload as a parameter.

type UploadPolicies

type UploadPolicies struct {
	URL  string
	Form map[string]string
}

UploadPolicies Amazon s3 upload policies

Jump to

Keyboard shortcuts

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