feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
|
||||
appconfig "mtusi2026-messenger/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnavailable = errors.New("s3 unavailable")
|
||||
ErrLoadConfig = errors.New("load aws config")
|
||||
ErrHeadBucket = errors.New("head bucket")
|
||||
ErrCreateBucket = errors.New("create bucket")
|
||||
ErrObjectNotFound = errors.New("object not found")
|
||||
ErrPutObject = errors.New("put object")
|
||||
ErrGetObject = errors.New("get object")
|
||||
ErrListObjects = errors.New("list objects")
|
||||
ErrDeleteObject = errors.New("delete object")
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
bucket string
|
||||
bootstrap *awss3.Client
|
||||
api *awss3.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type ListResult struct {
|
||||
Objects []ObjectInfo
|
||||
Pages int
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg appconfig.S3Config, log *slog.Logger) (*Client, error) {
|
||||
apiAccessKey, err := cfg.APIAccessKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 api access key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiSecretKey, err := cfg.APISecretKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 api secret key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrapAccessKey, err := cfg.BootstrapAccessKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 bootstrap access key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrapSecretKey, err := cfg.BootstrapSecretKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 bootstrap secret key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrap, err := newS3Client(ctx, log, cfg.Endpoint, cfg.Region, bootstrapAccessKey, bootstrapSecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api, err := newS3Client(ctx, log, cfg.Endpoint, cfg.Region, apiAccessKey, apiSecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
bucket: cfg.Bucket,
|
||||
bootstrap: bootstrap,
|
||||
api: api,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Bucket() string {
|
||||
return c.bucket
|
||||
}
|
||||
|
||||
func (c *Client) EnsureBucket(ctx context.Context) error {
|
||||
_, err := c.bootstrap.HeadBucket(ctx, &awss3.HeadBucketInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !isNotFound(err) {
|
||||
c.log.Error("head bucket", "bucket", c.bucket, "error", err)
|
||||
return ErrHeadBucket
|
||||
}
|
||||
|
||||
_, err = c.bootstrap.CreateBucket(ctx, &awss3.CreateBucketInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
})
|
||||
if err == nil || isAlreadyExists(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.log.Error("create bucket", "bucket", c.bucket, "error", err)
|
||||
return ErrCreateBucket
|
||||
}
|
||||
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
_, err := c.api.HeadBucket(ctx, &awss3.HeadBucketInput{Bucket: aws.String(c.bucket)})
|
||||
if err != nil {
|
||||
c.log.Error("head bucket", "bucket", c.bucket, "error", err)
|
||||
return operationError(ErrHeadBucket, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Put(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
body io.ReadSeeker,
|
||||
size int64,
|
||||
contentType string,
|
||||
) error {
|
||||
_, err := c.api.PutObject(ctx, &awss3.PutObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: body,
|
||||
ContentLength: aws.Int64(size),
|
||||
ContentType: aws.String(contentType),
|
||||
})
|
||||
if err != nil {
|
||||
c.log.Error("put object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return operationError(ErrPutObject, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Get(ctx context.Context, key string) (*awss3.GetObjectOutput, error) {
|
||||
out, err := c.api.GetObject(ctx, &awss3.GetObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if isNotFound(err) {
|
||||
c.log.Warn("get object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return nil, ErrObjectNotFound
|
||||
}
|
||||
if err != nil {
|
||||
c.log.Error("get object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return nil, operationError(ErrGetObject, err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListAll(ctx context.Context, prefix string) (ListResult, error) {
|
||||
result := ListResult{Objects: []ObjectInfo{}}
|
||||
|
||||
paginator := awss3.NewListObjectsV2Paginator(c.bootstrap, &awss3.ListObjectsV2Input{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Prefix: aws.String(prefix),
|
||||
})
|
||||
|
||||
for paginator.HasMorePages() {
|
||||
page, err := paginator.NextPage(ctx)
|
||||
if err != nil {
|
||||
c.log.Error("list objects", "bucket", c.bucket, "prefix", prefix, "error", err)
|
||||
return ListResult{}, ErrListObjects
|
||||
}
|
||||
|
||||
result.Pages++
|
||||
|
||||
for _, obj := range page.Contents {
|
||||
result.Objects = append(result.Objects, ObjectInfo{
|
||||
Key: aws.ToString(obj.Key),
|
||||
Size: aws.ToInt64(obj.Size),
|
||||
LastModified: aws.ToTime(obj.LastModified),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteAdmin(ctx context.Context, key string) error {
|
||||
_, err := c.bootstrap.DeleteObject(ctx, &awss3.DeleteObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if isNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
c.log.Error("delete admin object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return ErrDeleteObject
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newS3Client(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
endpoint string,
|
||||
region string,
|
||||
accessKey string,
|
||||
secretKey string,
|
||||
) (*awss3.Client, error) {
|
||||
cfg, err := awsconfig.LoadDefaultConfig(
|
||||
ctx,
|
||||
awsconfig.WithRegion(region),
|
||||
awsconfig.WithCredentialsProvider(
|
||||
credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("load aws config", "endpoint", endpoint, "region", region, "error", err)
|
||||
return nil, ErrLoadConfig
|
||||
}
|
||||
|
||||
return awss3.NewFromConfig(cfg, func(o *awss3.Options) {
|
||||
o.BaseEndpoint = aws.String(endpoint)
|
||||
o.UsePathStyle = true
|
||||
}), nil
|
||||
}
|
||||
|
||||
func operationError(operation, err error) error {
|
||||
var networkError net.Error
|
||||
var responseError *awshttp.ResponseError
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.As(err, &networkError) ||
|
||||
(errors.As(err, &responseError) && responseError.HTTPStatusCode() >= 500) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return operation
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool {
|
||||
var responseErr *awshttp.ResponseError
|
||||
if errors.As(err, &responseErr) && responseErr.HTTPStatusCode() == http.StatusNotFound {
|
||||
return true
|
||||
}
|
||||
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NotFound", "NoSuchBucket", "NoSuchKey":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isAlreadyExists(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "BucketAlreadyOwnedByYou", "BucketAlreadyExists":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
seaweedUID = 1000
|
||||
seaweedGID = 1000
|
||||
)
|
||||
|
||||
type Credential struct {
|
||||
AccessKey string `json:"accessKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
}
|
||||
|
||||
type Identity struct {
|
||||
Name string `json:"name"`
|
||||
Credentials []Credential `json:"credentials"`
|
||||
Actions []string `json:"actions"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Identities []Identity `json:"identities"`
|
||||
}
|
||||
|
||||
func WriteConfig(output string, cfg config.S3Config) error {
|
||||
apiAccessKey, err := cfg.APIAccessKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiSecretKey, err := cfg.APISecretKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bootstrapAccessKey, err := cfg.BootstrapAccessKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bootstrapSecretKey, err := cfg.BootstrapSecretKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config := Config{
|
||||
Identities: []Identity{
|
||||
{
|
||||
Name: "bootstrap",
|
||||
Credentials: []Credential{
|
||||
{AccessKey: bootstrapAccessKey, SecretKey: bootstrapSecretKey},
|
||||
},
|
||||
Actions: []string{"Admin", "Read", "Write", "List", "Tagging"},
|
||||
},
|
||||
{
|
||||
Name: "api",
|
||||
Credentials: []Credential{
|
||||
{AccessKey: apiAccessKey, SecretKey: apiSecretKey},
|
||||
},
|
||||
Actions: []string{"Read", "Write", "List", "Tagging"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal s3 config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil {
|
||||
return fmt.Errorf("create config directory: %w", err)
|
||||
}
|
||||
|
||||
tmp := output + ".tmp"
|
||||
if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil {
|
||||
return fmt.Errorf("write config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Chown(tmp, seaweedUID, seaweedGID); err != nil {
|
||||
return fmt.Errorf("chown config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Chmod(tmp, 0o400); err != nil {
|
||||
return fmt.Errorf("chmod config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Rename(tmp, output); err != nil {
|
||||
return fmt.Errorf("replace config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user