sssly

package module
v0.0.0-...-f751ff9 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: MPL-2.0 Imports: 14 Imported by: 0

README

Sssly is a simple wrapper for AWS S3 access.

package main

import (
	"io"
	"fmt"
	"github.com/luisfurquim/sssly"
)

func main() {
	var cli *sssly.Sssly
	var err error
	var wr io.WriteCloser
	var rd io.ReadCloser
	var dir []string
	var buf []byte

	cli, err = sssly.New(sssly.Opt{
		"region": "sa-east-1", // or "aws-cn-global", etc
		"credentials": "credentials", // filenames (comma separated) of credential files, see file format in comment below
		"profile": "myprofile", // profile name (section in the credential files)
		"bucket": "mybucket", // bucket name in S3
		"endpoint": "https://example.com", // URL of S3 endpoint
		"base-path": "temp/", // base pathname in S3 bucket, all keys provided in method callings will be appended to base-path
	})

/*

[myprofile]
aws_access_key_id = the_id_provided_by_the_S3_admin
aws_secret_access_key = secretsecretsecretsecretsecretsecretsecretsecret

[myotherprofile]
aws_access_key_id = the_other_id_provided_by_the_S3_admin
aws_secret_access_key = othersecretothersecretothersecretothersecret

*/

	if err != nil {
		// whatever
	}

	// List files in bucket
	dir, err = cli.Dir()
	if err != nil {
		// whatever
	}

	// Delete file(s) in bucket/base-path
	err = cli.Delete("some_junk_file.txt", ...)
	if err != nil {
		// whatever
	}

	// Create file in bucket/base-path a return its WriteCloser
	// Note: it bufferizes all content in memory, don't use to transfer huge files (use Upload for these cases)
	wr = cli.NewWriteCloser("some_new_file.txt")
	fmt.Fprintf(wr, "some file content")

	err = wr.Close() // MUST be called, otherwise no data is transfered to S3
	if err != nil {
		// whatever
	}

	// Uploads file to bucket/base-path, it does not buffers in memory, so it is suitable for huge files
	err = cli.Upload("s3-file.txt", "local-file.txt")
	if err != nil {
		// whatever
	}

	// Opens a file located in bucket/base-path and returns an io.Reader
	rd, err = cli.NewReadCloser("some_new_file.txt")
	if err != nil {
		// whatever
	}
	// Don't forget to close it!
	defer rd.Close()

	buf, err = io.ReadAll(rd)
	if err != nil {
		// whatever
	}

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrOptionRequiredBasePath error = errors.New("Option required: base path")
View Source
var ErrOptionRequiredBucket error = errors.New("Option required: bucket")
View Source
var ErrOptionRequiredCredentials error = errors.New("Option required: credentials")
View Source
var ErrOptionRequiredEndpoint error = errors.New("Option required: endpoint")
View Source
var ErrOptionRequiredProfile error = errors.New("Option required: profile")
View Source
var ErrOptionRequiredRegion error = errors.New("Option required: region")
View Source
var ErrOptionWrongTypeBasePath error = errors.New("Option wrong type: base path")
View Source
var ErrOptionWrongTypeBucket error = errors.New("Option wrong type: bucket")
View Source
var ErrOptionWrongTypeCredentials error = errors.New("Option wrong type: credentials")
View Source
var ErrOptionWrongTypeEndpoint error = errors.New("Option wrong type: endpoint")
View Source
var ErrOptionWrongTypeHttpClient error = errors.New("Option wrong type: http client")
View Source
var ErrOptionWrongTypeHttpTransport error = errors.New("Option wrong type: http transport")
View Source
var ErrOptionWrongTypeProfile error = errors.New("Option wrong type: profile")
View Source
var ErrOptionWrongTypeRegion error = errors.New("Option wrong type: region")

Functions

This section is empty.

Types

type GooseG

type GooseG struct {
	Init    goose.Alert
	Storage goose.Alert
}
var Goose GooseG = GooseG{
	Init:    goose.Alert(2),
	Storage: goose.Alert(2),
}

type Opt

type Opt map[string]interface{}

type Sssly

type Sssly struct {
	Bucket   string
	BasePath string
	Client   *s3.Client
}

func New

func New(opt Opt) (*Sssly, error)

func (*Sssly) Delete

func (s *Sssly) Delete(keys ...string) error

func (*Sssly) Dir

func (s *Sssly) Dir() ([]string, error)

func (*Sssly) Download

func (s *Sssly) Download(fname, key string) (int64, error)

func (*Sssly) NewReadCloser

func (s *Sssly) NewReadCloser(key string) (io.ReadCloser, error)

func (*Sssly) NewWriteCloser

func (s *Sssly) NewWriteCloser(key string) *WriteCloser

func (*Sssly) Upload

func (s *Sssly) Upload(key, fname string) error

func (*Sssly) UploadFromReader

func (s *Sssly) UploadFromReader(key string, rd io.Reader, sz int64) error

type WriteCloser

type WriteCloser struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func (*WriteCloser) Close

func (wr *WriteCloser) Close() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳