s3

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

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

Go to latest
Published: Sep 10, 2024 License: MIT Imports: 6 Imported by: 0

README

Go S3 client

Extended MinIO S3 client with (de)serialization.

Added extra functionality:

  • PutBytes, ReadBytes functions: for more convenient storage of bytes
  • Put, Read functions: for storage of structs (serialized in JSON/MsgPack/etc...)
  • BucketClient: for encapsulating functionality around specific bucket

Usage

package main

import (
	"bytes"
	"context"
	"encoding/hex"
	"fmt"
	"github.com/jackcvr/s3"
	"github.com/jackcvr/s3/msgpack"
	"github.com/minio/minio-go/v7"
	"os"
)

const (
	bucketName = "test-bucket"
	objectName = "test-object"
)

func main() {
	var accessKey = os.Getenv("ACCESS_KEY")
	var secretKey = os.Getenv("SECRET_KEY")

	// create BucketClient to simplify working with a single bucket
	c, err := s3.NewBucketClient(
		"localhost:9000",
		bucketName,
		s3.NewOptions(accessKey, secretKey, "", false),
		msgpack.MsgPackSerializer{}) // use MsgPack serializer for this bucket
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	// create bucket if it doesn't exist
	if err = c.EnsureBucket(ctx, minio.MakeBucketOptions{Region: "us-east-1"}); err != nil {
		panic(err)
	}

	// Put bytes
	if _, err = c.PutBytes(ctx, objectName, []byte("test"), minio.PutObjectOptions{}); err != nil {
		panic(err)
	}
	// Get bytes
	var buf bytes.Buffer
	if err = c.ReadBytes(ctx, objectName, &buf, minio.GetObjectOptions{}); err != nil {
		panic(err)
	}
	fmt.Println(string(buf.Bytes())) // test

	type Test struct {
		Name   string `json:"name"`
		Amount int    `json:"amount"`
	}

	// Put serialized struct
	obj := Test{
		Name:   "Test",
		Amount: 12,
	}
	if _, err = c.Put(ctx, objectName, obj, minio.PutObjectOptions{}); err != nil {
		panic(err)
	}

	// Get deserialized struct
	obj2 := Test{}
	if err = c.Read(ctx, objectName, &obj2, minio.GetObjectOptions{}); err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", obj2) // {Name:Test Amount:12}

	buf.Reset()
	if err = c.ReadBytes(ctx, objectName, &buf, minio.GetObjectOptions{}); err != nil {
		panic(err)
	}
	fmt.Println(hex.Dump(buf.Bytes()))
	//00000000  82 a4 4e 61 6d 65 a4 54  65 73 74 a6 41 6d 6f 75  |..Name.Test.Amou|
	//00000010  6e 74 0c                                          |nt.|
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewOptions

func NewOptions(accessKeyID, secretAccessKey, sessionToken string, secure bool) *minio.Options

Types

type BucketClient

type BucketClient struct {
	Client     *Client
	BucketName string
	Serializer Serializer
}

func NewBucketClient

func NewBucketClient(endpoint string, bucketName string, opts *minio.Options, serializer Serializer) (*BucketClient, error)

func (*BucketClient) EnsureBucket

func (c *BucketClient) EnsureBucket(ctx context.Context, opts minio.MakeBucketOptions) error

func (*BucketClient) GetObject

func (c *BucketClient) GetObject(ctx context.Context, objectName string, opts minio.GetObjectOptions) (*minio.Object, error)

func (*BucketClient) ListObjects

func (c *BucketClient) ListObjects(ctx context.Context, opts minio.ListObjectsOptions) <-chan minio.ObjectInfo

func (*BucketClient) Put

func (c *BucketClient) Put(ctx context.Context, objectName string, value any, opts minio.PutObjectOptions, s ...Serializer) (minio.UploadInfo, error)

func (*BucketClient) PutBytes

func (c *BucketClient) PutBytes(ctx context.Context, objectName string, value []byte, opts minio.PutObjectOptions) (minio.UploadInfo, error)

func (*BucketClient) Read

func (c *BucketClient) Read(ctx context.Context, objectName string, dst any, opts minio.GetObjectOptions, s ...Serializer) error

func (*BucketClient) ReadBytes

func (c *BucketClient) ReadBytes(ctx context.Context, objectName string, dst io.Writer, opts minio.GetObjectOptions) error

func (*BucketClient) RemoveBucket

func (c *BucketClient) RemoveBucket(ctx context.Context) error

func (*BucketClient) RemoveObject

func (c *BucketClient) RemoveObject(ctx context.Context, objectName string, opts minio.RemoveObjectOptions) error

func (*BucketClient) StatObject

func (c *BucketClient) StatObject(ctx context.Context, objectName string, opts minio.GetObjectOptions) (minio.ObjectInfo, error)

type Client

type Client struct {
	*minio.Client
	Serializer Serializer
}

func NewClient

func NewClient(endpoint string, opts *minio.Options, serializer Serializer) (*Client, error)

func (*Client) EnsureBucket

func (c *Client) EnsureBucket(ctx context.Context, bucketName string, opts minio.MakeBucketOptions) error

func (*Client) GetBucketClient

func (c *Client) GetBucketClient(bucketName string, serializer Serializer) *BucketClient

func (*Client) Put

func (c *Client) Put(ctx context.Context, bucketName, objectName string, value any, opts minio.PutObjectOptions, s ...Serializer) (minio.UploadInfo, error)

func (*Client) PutBytes

func (c *Client) PutBytes(ctx context.Context, bucketName, objectName string, value []byte, opts minio.PutObjectOptions) (minio.UploadInfo, error)

func (*Client) Read

func (c *Client) Read(ctx context.Context, bucketName string, objectName string, dst any, opts minio.GetObjectOptions, s ...Serializer) error

func (*Client) ReadBytes

func (c *Client) ReadBytes(ctx context.Context, bucketName string, objectName string, dst io.Writer, opts minio.GetObjectOptions) error

type JSONSerializer

type JSONSerializer struct{}

func (JSONSerializer) Deserialize

func (_ JSONSerializer) Deserialize(data []byte, v any) error

func (JSONSerializer) Serialize

func (_ JSONSerializer) Serialize(v any) ([]byte, error)

type Serializer

type Serializer interface {
	Serialize(any) ([]byte, error)
	Deserialize([]byte, any) error
}

Directories

Path Synopsis
msgpack module

Jump to

Keyboard shortcuts

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