forked from mtusi2026/mtusi2026lab5
Перенес изменения
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
Client *s3.Client
|
||||
Bucket string
|
||||
}
|
||||
|
||||
func New(c *config.Attachment) *Store {
|
||||
return &Store{
|
||||
Bucket: c.Bucket,
|
||||
Client: s3.New(
|
||||
s3.Options{
|
||||
Region: c.Region,
|
||||
BaseEndpoint: aws.String(c.Endpoint),
|
||||
UsePathStyle: true,
|
||||
Credentials: credentials.NewStaticCredentialsProvider(
|
||||
c.AccessKey,
|
||||
c.SecretKey,
|
||||
"",
|
||||
),
|
||||
RetryMaxAttempts: 1,
|
||||
HTTPClient: &http.Client{Timeout: 30 * time.Second},
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) Put(ctx context.Context, key, mime string, r io.Reader, size int64) error {
|
||||
_, err := s.Client.PutObject(ctx, &s3.PutObjectInput{Bucket: &s.Bucket, Key: &key, Body: r, ContentLength: &size, ContentType: &mime})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) Get(ctx context.Context, key string) (*s3.GetObjectOutput, error) {
|
||||
return s.Client.GetObject(ctx, &s3.GetObjectInput{Bucket: &s.Bucket, Key: &key})
|
||||
}
|
||||
|
||||
func (s *Store) Head(ctx context.Context, key string) (*s3.HeadObjectOutput, error) {
|
||||
return s.Client.HeadObject(ctx, &s3.HeadObjectInput{Bucket: &s.Bucket, Key: &key})
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, key string) error {
|
||||
_, e := s.Client.DeleteObject(ctx, &s3.DeleteObjectInput{Bucket: &s.Bucket, Key: &key})
|
||||
return e
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
_, e := s.Client.HeadBucket(ctx, &s3.HeadBucketInput{Bucket: &s.Bucket})
|
||||
return e
|
||||
}
|
||||
|
||||
func Missing(err error) bool {
|
||||
var api smithy.APIError
|
||||
return errors.As(err, &api) && (api.ErrorCode() == "NoSuchKey" || api.ErrorCode() == "NotFound" || api.ErrorCode() == "404")
|
||||
}
|
||||
Reference in New Issue
Block a user