Skip to content

Commit

Permalink
Do not fail the object fetch when object size is unknown (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix authored Nov 25, 2024
1 parent dee3f54 commit 2cbfaa4
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions common/aws/s3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"errors"
"fmt"
"runtime"
"sync"

Expand All @@ -19,6 +18,10 @@ import (
"golang.org/x/sync/errgroup"
)

const (
defaultBlobBufferSizeByte = 128 * 1024
)

var (
once sync.Once
ref *client
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2cbfaa4

Please sign in to comment.