s3

package
v0.0.0-...-c3eb3c2 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: Apache-2.0 Imports: 14 Imported by: 2

README

Example of use

Here is a basic example of how to use the Client

package main

import (
	"fmt"

	"github.com/PrimerAI/go-micro-utils-public/gmu/s3"
)

func main() {
	client, err := s3.NewClient(
        // AWS s3
		s3.WithEndpoint(""),
        // Minio
		// s3.WithEndpoint("http://127.0.0.1:9000"), 
		s3.WithS3ForcePathStyle(true),
	)
	if err != nil {
		print("panic")
		panic(err)
	}
	list, err := client.List(s3.Path{
		Bucket: "some-bucket",
		Key:    "some-dir",
	})
	if err != nil {
		print("panic")
		panic(err)
	}
	for _, ele := range list {
		fmt.Println(ele.Key)
	}
}

This code will list the keys under s3://some-bucket/some-dir.

AWS

For normal AWS use s3.NewClient() should be enough

Minio

To use a MINIO server use ```go client, err := s3.NewClient( s3.WithEndpoint("minio server uri"), s3.WithS3ForcePathStyle(true), )

Note that `s3.WithS3ForcePathStyle(true)` is required

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	ListDirectories(p Path) ([]Path, error)
	List(p Path) ([]Path, error)
	Download(p Path) ([]byte, error)
	DownloadInFile(p Path, f *os.File) (int64, error)
	Copy(source Path, target Path) error
	Upload(b []byte, p Path) error
	DeleteRecursive(p Path) error
	DeleteBucket(name string, log logr.Logger) error
	DeleteObject(p Path) error
	Exists(p Path) bool
	CreateBucket(name string) error
	AddBucketTag(bucketName string, tags map[string]string, overwrite bool) error
	BlockBucketPublicAccess(name string) error
	BucketServerSideEncryption(bucket string) error
}

Client is designed to be an Storage Interface for S3 s3Client

func NewClient

func NewClient(options ...ClientOption) (Client, error)

NewClient initializes the s3Client struct with a new session

type ClientOption

type ClientOption func(*aws.Config)

ClientOption is desing to allow user overwrites to the aws config

func WithCredentials

func WithCredentials(cred *credentials.Credentials) ClientOption

WithCredentials allows overwriting the credentials / useful for minio server see: https://docs.min.io/docs/how-to-use-aws-sdk-for-go-with-minio-server.html

func WithDisableSSL

func WithDisableSSL(ssl bool) ClientOption

WithDisableSSL disables SSL when set to true

func WithEndpoint

func WithEndpoint(endpoint string) ClientOption

WithEndpoint allows to overwrite the aws endpoint / useful for minio server

func WithRegion

func WithRegion(region string) ClientOption

WithRegion allows to set the aws region

func WithS3ForcePathStyle

func WithS3ForcePathStyle(force bool) ClientOption

WithS3ForcePathStyle allows to set S3ForcePathStyle

type ParseError

type ParseError string

ParseError is to have an error with the apropiate location

func (ParseError) Error

func (s ParseError) Error() string

Error is to make the error msg

type Path

type Path struct {
	Bucket string
	Key    string
}

Path is to store a bucket and key for s3 purposes

func Append

func Append(p Path, key string) Path

Append adds a path to an existing s3.Path

func FromURI

func FromURI(uri string) (Path, error)

FromURI accepts an s3 uri of format `s3://<bucket>(/<key>, optional)` where the key can have slashes, `/`. Returns the bucket and key in a struct, Path, with fields of those names, and an error if there was trouble parsing the passed uri.

func (Path) Join

func (p Path) Join() string

Join is a helper method to ensure that there are no leading or training slashes, `/` on the Path's bucket and key, then joins them into a single path returning the result as a string.

func (Path) ToURI

func (p Path) ToURI() string

ToURI joins the bucket and key together into a single path, cleaning leading and training slashes `/`, as well as prepending the S3 scheme, `s3://`, returning a single string.

Jump to

Keyboard shortcuts

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