lab4: исправления по ревью
This commit is contained in:
@@ -28,5 +28,5 @@ type Store interface {
|
||||
Copy(ctx context.Context, src, dst, contentType string, metadata map[string]string) error
|
||||
Delete(ctx context.Context, key string) error
|
||||
List(ctx context.Context, prefix string) ([]Object, error)
|
||||
PresignPut(ctx context.Context, key string, ttl time.Duration) (string, time.Time, error)
|
||||
PresignPut(ctx context.Context, key string, size int64, ttl time.Duration) (string, time.Time, error)
|
||||
}
|
||||
|
||||
@@ -229,13 +229,15 @@ func (s *S3) List(ctx context.Context, prefix string) ([]Object, error) {
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
// PresignPut signs only the bucket, key and host. Deliberately no Content-Type
|
||||
// or checksum: adding them to the signed headers forces every client to send
|
||||
// exactly those headers, and SeaweedFS does not validate checksums anyway.
|
||||
// Completion verifies the actual bytes instead.
|
||||
// PresignPut signs the bucket, key, host and exact Content-Length. The size is
|
||||
// therefore enforced by S3 at write time: a client that uploads a different
|
||||
// number of bytes gets 403 SignatureDoesNotMatch. Content-Type and checksums
|
||||
// are not signed (SeaweedFS does not validate checksums); completion verifies
|
||||
// the actual bytes and their structure.
|
||||
func (s *S3) PresignPut(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
size int64,
|
||||
ttl time.Duration,
|
||||
) (string, time.Time, error) {
|
||||
expiresAt := time.Now().UTC().Add(ttl)
|
||||
@@ -243,8 +245,9 @@ func (s *S3) PresignPut(
|
||||
presigned, err := s3.NewPresignClient(s.public).PresignPutObject(
|
||||
ctx,
|
||||
&s3.PutObjectInput{
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
Bucket: aws.String(s.bucket),
|
||||
Key: aws.String(key),
|
||||
ContentLength: aws.Int64(size),
|
||||
},
|
||||
func(options *s3.PresignOptions) {
|
||||
options.Expires = ttl
|
||||
@@ -270,7 +273,9 @@ func mapObjectError(operation, subject string, err error) error {
|
||||
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NotFound", "NoSuchKey", "NoSuchBucket":
|
||||
case "NoSuchBucket":
|
||||
return fmt.Errorf("%w: %s %q: %w", domain.ErrUnavailable, operation, subject, err)
|
||||
case "NotFound", "NoSuchKey":
|
||||
return fmt.Errorf("%s %q: %w", operation, subject, domain.ErrNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/aws/smithy-go"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
func TestMapObjectErrorBucketIsUnavailable(t *testing.T) {
|
||||
err := mapObjectError("put object", "key", &smithy.GenericAPIError{
|
||||
Code: "NoSuchBucket",
|
||||
Message: "bucket is missing",
|
||||
})
|
||||
require.ErrorIs(t, err, domain.ErrUnavailable)
|
||||
}
|
||||
|
||||
func TestMapObjectErrorMissingKeyIsNotFound(t *testing.T) {
|
||||
err := mapObjectError("open object", "key", &smithy.GenericAPIError{
|
||||
Code: "NoSuchKey",
|
||||
Message: "object is missing",
|
||||
})
|
||||
require.ErrorIs(t, err, domain.ErrNotFound)
|
||||
}
|
||||
Reference in New Issue
Block a user