fix(s3): handle storage outages and check bucket readiness

This commit is contained in:
kk10
2026-11-05 11:04:41 +03:00
parent 592594ae94
commit d467cab01a
10 changed files with 127 additions and 22 deletions
+23 -2
View File
@@ -5,6 +5,7 @@ import (
"errors"
"io"
"log/slog"
"net"
"net/http"
"time"
@@ -19,6 +20,7 @@ import (
)
var (
ErrUnavailable = errors.New("s3 unavailable")
ErrLoadConfig = errors.New("load aws config")
ErrHeadBucket = errors.New("head bucket")
ErrCreateBucket = errors.New("create bucket")
@@ -118,6 +120,15 @@ func (c *Client) EnsureBucket(ctx context.Context) error {
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,
@@ -134,7 +145,7 @@ func (c *Client) Put(
})
if err != nil {
c.log.Error("put object", "bucket", c.bucket, "key", key, "error", err)
return ErrPutObject
return operationError(ErrPutObject, err)
}
return nil
@@ -151,7 +162,7 @@ func (c *Client) Get(ctx context.Context, key string) (*awss3.GetObjectOutput, e
}
if err != nil {
c.log.Error("get object", "bucket", c.bucket, "key", key, "error", err)
return nil, ErrGetObject
return nil, operationError(ErrGetObject, err)
}
return out, nil
@@ -228,6 +239,16 @@ func newS3Client(
}), 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 {