forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
package blobstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("object not found")
|
||||
|
||||
type Store interface {
|
||||
Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string) error
|
||||
Get(ctx context.Context, key string) (io.ReadCloser, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
Ping(ctx context.Context) error
|
||||
List(ctx context.Context, prefix string, fn func(key string, modified time.Time) error) error
|
||||
Exists(ctx context.Context, key string) (bool, error)
|
||||
}
|
||||
|
||||
type S3Config struct {
|
||||
Endpoint, Region, Bucket, AccessKey, SecretKey string
|
||||
}
|
||||
|
||||
type S3 struct {
|
||||
client *s3.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewS3(ctx context.Context, c S3Config) (*S3, error) {
|
||||
if c.Endpoint == "" || c.Bucket == "" || c.AccessKey == "" || c.SecretKey == "" {
|
||||
return nil, errors.New("incomplete S3 configuration")
|
||||
}
|
||||
cfg, err := config.LoadDefaultConfig(ctx,
|
||||
config.WithRegion(c.Region),
|
||||
config.WithRetryMaxAttempts(2),
|
||||
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(c.AccessKey, c.SecretKey, "")),
|
||||
config.WithRequestChecksumCalculation(aws.RequestChecksumCalculationWhenRequired),
|
||||
config.WithResponseChecksumValidation(aws.ResponseChecksumValidationWhenRequired),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
|
||||
o.BaseEndpoint = aws.String(c.Endpoint)
|
||||
o.UsePathStyle = true
|
||||
})
|
||||
return &S3{client: client, bucket: c.Bucket}, nil
|
||||
}
|
||||
|
||||
func (s *S3) Put(ctx context.Context, key string, body io.ReadSeeker, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: body,
|
||||
ContentLength: aws.Int64(size),
|
||||
ContentType: aws.String(contentType),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *S3) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
out, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return out.Body, nil
|
||||
}
|
||||
|
||||
func (s *S3) Delete(ctx context.Context, key string) error {
|
||||
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *S3) Ping(ctx context.Context) error {
|
||||
body := []byte("ok")
|
||||
if _, err := s.client.PutObject(ctx, &s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String("_health/probe"),
|
||||
Body: bytes.NewReader(body),
|
||||
ContentLength: aws.Int64(int64(len(body))),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
out, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String("_health/probe"),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Body.Close()
|
||||
_, err = io.Copy(io.Discard, out.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *S3) List(ctx context.Context, prefix string, fn func(key string, modified time.Time) error) error {
|
||||
p := s3.NewListObjectsV2Paginator(s.client, &s3.ListObjectsV2Input{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Prefix: aws.String(prefix),
|
||||
})
|
||||
for p.HasMorePages() {
|
||||
page, err := p.NextPage(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, o := range page.Contents {
|
||||
if err := fn(aws.ToString(o.Key), aws.ToTime(o.LastModified)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *S3) Exists(ctx context.Context, key string) (bool, error) {
|
||||
_, err := s.client.HeadObject(ctx, &s3.HeadObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if err != nil {
|
||||
if isNotFound(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NoSuchKey", "NotFound":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Memory struct {
|
||||
mu sync.Mutex
|
||||
m map[string][]byte
|
||||
mod map[string]time.Time
|
||||
Err error
|
||||
}
|
||||
|
||||
func NewMemory() *Memory { return &Memory{m: map[string][]byte{}, mod: map[string]time.Time{}} }
|
||||
|
||||
func (s *Memory) Put(_ context.Context, key string, body io.ReadSeeker, _ int64, _ string) error {
|
||||
if s.Err != nil {
|
||||
return s.Err
|
||||
}
|
||||
b, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.m[key], s.mod[key] = b, time.Now()
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Memory) Get(_ context.Context, key string) (io.ReadCloser, error) {
|
||||
if s.Err != nil {
|
||||
return nil, s.Err
|
||||
}
|
||||
s.mu.Lock()
|
||||
b, ok := s.m[key]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return io.NopCloser(bytes.NewReader(b)), nil
|
||||
}
|
||||
|
||||
func (s *Memory) Delete(_ context.Context, key string) error {
|
||||
if s.Err != nil {
|
||||
return s.Err
|
||||
}
|
||||
s.mu.Lock()
|
||||
delete(s.m, key)
|
||||
delete(s.mod, key)
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Memory) Ping(_ context.Context) error { return s.Err }
|
||||
|
||||
func (s *Memory) Exists(_ context.Context, key string) (bool, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
_, ok := s.m[key]
|
||||
return ok, s.Err
|
||||
}
|
||||
|
||||
func (s *Memory) List(_ context.Context, prefix string, fn func(string, time.Time) error) error {
|
||||
s.mu.Lock()
|
||||
keys := make([]string, 0, len(s.m))
|
||||
for k := range s.m {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
s.mu.Unlock()
|
||||
for _, k := range keys {
|
||||
if err := fn(k, s.mod[k]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Memory) Len() int { s.mu.Lock(); defer s.mu.Unlock(); return len(s.m) }
|
||||
Reference in New Issue
Block a user