Skip to content

Commit

Permalink
Include content length in the response of Get and GetRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ashwanthgoli committed Oct 8, 2024
1 parent 8897e65 commit 369f1ef
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 23 deletions.
10 changes: 10 additions & 0 deletions objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,13 @@ func (t *timingReaderWriterTo) WriteTo(w io.Writer) (n int64, err error) {
t.timingReader.updateMetrics(int(n), err)
return n, err
}

type ObjectSizerReadCloser struct {
io.ReadCloser
Size int64
}

// ObjectSize implement ObjectSizer.
func (o ObjectSizerReadCloser) ObjectSize() (int64, error) {
return o.Size, nil
}
2 changes: 1 addition & 1 deletion providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (b *Bucket) getBlobReader(ctx context.Context, name string, httpRange blob.
return nil, errors.Wrapf(err, "cannot download blob, address: %s", blobClient.URL())
}
retryOpts := azblob.RetryReaderOptions{MaxRetries: int32(b.readerMaxRetries)}
return resp.NewRetryReader(ctx, &retryOpts), nil
return objstore.ObjectSizerReadCloser{ReadCloser: resp.NewRetryReader(ctx, &retryOpts), Size: *resp.ContentLength}, nil
}

// Get returns a reader for the given object name.
Expand Down
2 changes: 1 addition & 1 deletion providers/bos/bos.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func (b *Bucket) getRange(_ context.Context, bucketName, objectKey string, off,
return nil, err
}

return obj.Body, nil
return objstore.ObjectSizerReadCloser{ReadCloser: obj.Body, Size: obj.ContentLength}, err
}

func configFromEnv() Config {
Expand Down
12 changes: 1 addition & 11 deletions providers/cos/cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,10 @@ func (b *Bucket) getRange(ctx context.Context, name string, off, length int64) (
return nil, err
}
// Add size info into reader to pass it to Upload function.
r := objectSizerReadCloser{ReadCloser: resp.Body, size: resp.ContentLength}
r := objstore.ObjectSizerReadCloser{ReadCloser: resp.Body, Size: resp.ContentLength}
return r, nil
}

type objectSizerReadCloser struct {
io.ReadCloser
size int64
}

// ObjectSize implement objstore.ObjectSizer.
func (o objectSizerReadCloser) ObjectSize() (int64, error) {
return o.size, nil
}

// Get returns a reader for the given object name.
func (b *Bucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
return b.getRange(ctx, name, 0, -1)
Expand Down
13 changes: 10 additions & 3 deletions providers/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ func (b *Bucket) GetRange(ctx context.Context, name string, off, length int64) (
return nil, errors.New("object name is empty")
}

file := filepath.Join(b.rootDir, name)
if _, err := os.Stat(file); err != nil {
var (
file = filepath.Join(b.rootDir, name)
stat os.FileInfo
err error
)
if stat, err = os.Stat(file); err != nil {
return nil, errors.Wrapf(err, "stat %s", file)
}

Expand All @@ -171,7 +175,10 @@ func (b *Bucket) GetRange(ctx context.Context, name string, off, length int64) (
return f, nil
}

return &rangeReaderCloser{Reader: io.LimitReader(f, length), f: f}, nil
return objstore.ObjectSizerReadCloser{
ReadCloser: &rangeReaderCloser{Reader: io.LimitReader(f, length),
f: f,
}, Size: stat.Size()}, nil
}

// Exists checks if the given directory exists in memory.
Expand Down
14 changes: 12 additions & 2 deletions providers/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,22 @@ func (b *Bucket) Iter(ctx context.Context, dir string, f func(string) error, opt

// Get returns a reader for the given object name.
func (b *Bucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
return b.bkt.Object(name).NewReader(ctx)
r, err := b.bkt.Object(name).NewReader(ctx)
if err != nil {
return r, err
}

return objstore.ObjectSizerReadCloser{ReadCloser: r, Size: r.Attrs.Size}, nil
}

// GetRange returns a new range reader for the given object name and range.
func (b *Bucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) {
return b.bkt.Object(name).NewRangeReader(ctx, off, length)
r, err := b.bkt.Object(name).NewRangeReader(ctx, off, length)
if err != nil {
return r, err
}

return objstore.ObjectSizerReadCloser{ReadCloser: r, Size: r.Attrs.Size}, nil
}

// Attributes returns information about the specified object.
Expand Down
2 changes: 1 addition & 1 deletion providers/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (b *Bucket) getRange(_ context.Context, name string, off, length int64) (io
if err != nil {
return nil, errors.Wrap(err, "failed to get object")
}
return output.Body, nil
return objstore.ObjectSizerReadCloser{ReadCloser: output.Body, Size: output.ContentLength}, nil
}

// Exists checks if the given object exists in the bucket.
Expand Down
4 changes: 2 additions & 2 deletions providers/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (b *Bucket) Get(ctx context.Context, name string) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
return response.Content, nil
return objstore.ObjectSizerReadCloser{ReadCloser: response.Content, Size: *response.ContentLength}, nil
}

// GetRange returns a new range reader for the given object name and range.
Expand Down Expand Up @@ -164,7 +164,7 @@ func (b *Bucket) GetRange(ctx context.Context, name string, offset, length int64
if err != nil {
return nil, err
}
return response.Content, nil
return objstore.ObjectSizerReadCloser{ReadCloser: response.Content, Size: *response.ContentLength}, nil
}

// Upload the contents of the reader as an object into the bucket.
Expand Down
11 changes: 9 additions & 2 deletions providers/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,17 @@ func (b *Bucket) getRange(ctx context.Context, name string, off, length int64) (
}

// StatObject to see if the object exists and we have permissions to read it
if _, err := b.client.StatObject(ctx, b.name, name, *opts); err != nil {
var stat minio.ObjectInfo
if stat, err = b.client.StatObject(ctx, b.name, name, *opts); err != nil {
return nil, err
}
return b.client.GetObject(ctx, b.name, name, *opts)

o, err := b.client.GetObject(ctx, b.name, name, *opts)
if err != nil {
return o, err
}

return objstore.ObjectSizerReadCloser{ReadCloser: o, Size: stat.Size}, nil
}

// Get returns a reader for the given object name.
Expand Down

0 comments on commit 369f1ef

Please sign in to comment.