github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/storage/storage_operations_s3.go (about)

     1  package storage
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/credentials"
    10  	"github.com/aws/aws-sdk-go/aws/session"
    11  	"github.com/aws/aws-sdk-go/service/s3"
    12  
    13  	"github.com/machinefi/w3bstream/pkg/depends/base/types"
    14  )
    15  
    16  type S3 struct {
    17  	Endpoint         string         `env:""`
    18  	Region           string         `env:""`
    19  	AccessKeyID      string         `env:""`
    20  	SecretAccessKey  types.Password `env:""`
    21  	SessionToken     string         `env:""`
    22  	BucketName       string         `env:""`
    23  	UrlExpire        types.Duration `env:""`
    24  	S3ForcePathStyle bool           `env:""`
    25  
    26  	cli *s3.S3
    27  }
    28  
    29  func (s *S3) Type() StorageType { return STORAGE_TYPE__S3 }
    30  
    31  func (s *S3) Init() error {
    32  	sess, err := session.NewSession(&aws.Config{
    33  		Endpoint:         aws.String(s.Endpoint),
    34  		Region:           aws.String(s.Region),
    35  		Credentials:      credentials.NewStaticCredentials(s.AccessKeyID, s.SecretAccessKey.String(), s.SessionToken),
    36  		S3ForcePathStyle: aws.Bool(s.S3ForcePathStyle),
    37  	})
    38  	if err != nil {
    39  		return err
    40  	}
    41  	s.cli = s3.New(sess)
    42  	return nil
    43  }
    44  
    45  func (s *S3) SetDefault() {
    46  	if s.UrlExpire == 0 {
    47  		s.UrlExpire = types.Duration(10 * time.Minute)
    48  	}
    49  }
    50  
    51  func (s *S3) IsZero() bool {
    52  	return s.Endpoint == "" ||
    53  		s.Region == "" ||
    54  		s.AccessKeyID == "" ||
    55  		s.SecretAccessKey == "" ||
    56  		s.BucketName == ""
    57  }
    58  
    59  func (s *S3) Name() string {
    60  	return "s3-cli"
    61  }
    62  
    63  func (s *S3) Upload(key string, data []byte, chk ...HmacAlgType) error {
    64  	input := &s3.PutObjectInput{
    65  		Bucket: aws.String(s.BucketName),
    66  		Key:    aws.String(key),
    67  		Body:   bytes.NewReader(data),
    68  	}
    69  
    70  	t := HMAC_ALG_TYPE__MD5
    71  	if len(chk) > 0 && chk[0] != 0 {
    72  		t = chk[0]
    73  	}
    74  
    75  	sum := t.Base64Sum(data)
    76  	switch t {
    77  	case HMAC_ALG_TYPE__MD5:
    78  		input.SetContentMD5(sum)
    79  	case HMAC_ALG_TYPE__SHA1:
    80  		input.SetChecksumAlgorithm(t.Type())
    81  		input.SetChecksumSHA1(sum)
    82  	case HMAC_ALG_TYPE__SHA256:
    83  		input.SetChecksumAlgorithm(t.Type())
    84  		input.SetChecksumSHA256(sum)
    85  	}
    86  
    87  	_, err := s.cli.PutObject(input)
    88  	return err
    89  }
    90  
    91  func (s *S3) Read(key string, chk ...HmacAlgType) (data []byte, sum []byte, err error) {
    92  	resp, err := s.cli.GetObject(&s3.GetObjectInput{
    93  		Bucket: aws.String(s.BucketName),
    94  		Key:    aws.String(key),
    95  	})
    96  	if err != nil {
    97  		return
    98  	}
    99  	defer resp.Body.Close()
   100  
   101  	data, err = io.ReadAll(resp.Body)
   102  	if err != nil {
   103  		return
   104  	}
   105  
   106  	t := HMAC_ALG_TYPE__MD5
   107  	if len(chk) > 0 && chk[0] != 0 {
   108  		t = chk[0]
   109  	}
   110  	sum = t.Sum(data)
   111  
   112  	return
   113  }
   114  
   115  func (s *S3) Delete(key string) error {
   116  	_, err := s.cli.DeleteObject(&s3.DeleteObjectInput{
   117  		Bucket: aws.String(s.BucketName),
   118  		Key:    aws.String(key),
   119  	})
   120  	return err
   121  }
   122  
   123  func (s *S3) DownloadUrl(key string) (string, error) {
   124  	req, _ := s.cli.GetObjectRequest(&s3.GetObjectInput{
   125  		Bucket: aws.String(s.BucketName),
   126  		Key:    aws.String(key),
   127  	})
   128  	return req.Presign(s.UrlExpire.Duration())
   129  }