diff --git a/common/aws/s3/client.go b/common/aws/s3/client.go index c3ae159d41..9dcaa346f4 100644 --- a/common/aws/s3/client.go +++ b/common/aws/s3/client.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "fmt" "runtime" "sync" @@ -19,6 +18,10 @@ import ( "golang.org/x/sync/errgroup" ) +const ( + defaultBlobBufferSizeByte = 128 * 1024 +) + var ( once sync.Once ref *client @@ -106,24 +109,13 @@ func NewClient(ctx context.Context, cfg commonaws.ClientConfig, logger logging.L return ref, err } -func PeekObjectSize(ctx context.Context, s3Client *s3.Client, bucket, key string) (int64, error) { - input := &s3.HeadObjectInput{ - Bucket: aws.String(bucket), - Key: aws.String(key), - } - result, err := s3Client.HeadObject(ctx, input) - if err != nil { - return 0, fmt.Errorf("failed to head object: %w", err) - } - return *result.ContentLength, nil -} - func (s *client) DownloadObject(ctx context.Context, bucket string, key string) ([]byte, error) { - size, err := PeekObjectSize(ctx, s.s3Client, bucket, key) - if err != nil { - return nil, err + objectSize := defaultBlobBufferSizeByte + size, err := s.HeadObject(ctx, bucket, key) + if err == nil { + objectSize = int(*size) } - buffer := manager.NewWriteAtBuffer(make([]byte, 0, size)) + buffer := manager.NewWriteAtBuffer(make([]byte, 0, objectSize)) var partMiBs int64 = 10 downloader := manager.NewDownloader(s.s3Client, func(d *manager.Downloader) {